3 ms·
Here's what I came up with. It compiles and executes but the result is a random symbol on the console. Definitely missing something. #include<iostream> #in
by mipnix 16y ago
Here's what I came up with. It compiles and executes but the result is a random symbol on the console. Definitely missing something.
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
void condense_by_removing(char *x, char y);
int main()
{
char *string;
string = new char[50];
cout << "ENter your name: ";
cin >> string;
cout << endl << "Enter the character to remove: ";
char ch;
cin >> ch;
condense_by_removing(string, ch);
cout << endl << "new string is: " << *string;
char dit;
cin >> dit;
return 0;
}
void condense_by_removing(char *z_terminated, char char_to_remove)
{
char *chPtr = new char;
char *trailPtr = new char;
for(int i = 0; i <= strlen(z_terminated); ++i)
{
chPtr = z_terminated;
if(z_terminated[i] == char_to_remove)
{
trailPtr = chPtr;
delete chPtr;
}
}
}
- RiderOfGiraffes 16y agoOK, now's the time to learn about debugging. Writing clean code to solve small problems from scratch is easy. Debugging existing code, especially well-written code, is hard. This is where you need to use your problem solving skills. Pretend that you're the computer and walk through the code in small steps, tediously detailed. Make sure you really understand what the code is doing. What exactly happens when you execute "char *chPtr = new char;" ? What exactly happens when you execute "delete chPtr;" ? And why do I say that debugging well-written code is especially hard? Because the names, clarity and simplicity in well-written code seduce you into the same mindset as produced the code that has the bug.
- mipnix 16y agoI could have used you as my professor in my recent data structures class. I imagine you have myriad responsibilities in the course of your day and I am grateful for the time you are affording me. I will work on this tonight.