Im i think i am very close....what did i do wrong

  • MHB
  • Thread starter needOfHelpCMath
  • Start date
In summary, the code provided has some problems in terms of logic and output. It attempts to replace any space in a 2-character string with an underscore, but there are issues with assigning characters to strings and the use of a bool variable. The corrected version only uses isspace(), at(), and if statements to check for spaces and replace them with underscores. The conversation also mentions that the code was written by someone with no background in programming and they express their gratitude for the help.
  • #1
needOfHelpCMath
72
0
Replace any space ' ' by '_' in 2-character string passCode. If no space exists, the program should not print anything. Sample output for the given program:
1_

Code:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
   string passCode;
   passCode = "1 ";
 
  if  (isspace(passCode.at(1))) {
  passCode = passCode.at(0);
  passCode = "_";
  
  }
cout << "1";
 
   cout << passCode << endl;
   return 0;
}

Testing: "1 "
Your output: 1_
✖ Testing: "a1"
Expected output: a1
Your output: 1a1
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
This code has some problems:
Code:
if  (isspace(passCode.at(1))) {
     passCode = passCode.at(0);
   passCode = "_";
  }
cout << "1";

passCode = passCode.at(0); This is an assignment of a character to a string. Actually, I was surprised that this didn't cause a crash. Turns out this is valid, but the result is that passCode is now a string of length 1 with only component 'a'.

Next you immediately execute passCode="_"; This is okay, but now passCode is the different string of length 1, namely "_".

Your specifications were that no output is made unless string passCode is changed. Here's a correct version:
Code:
int main() {
   string passCode;
   passCode = "1 ";
   bool changed = false;
   if (isspace(passCode.at(0))) {
      passCode.at(0) = '_';
      changed = true;
   }
   if (isspace(passCode.at(1))) {
      passCode.at(1) = '_';
      changed = true;
   }
   if (changed) {
      cout << passCode << endl;
   }
   return(0}
}
 
  • #3
johng said:
This code has some problems:
Code:
if  (isspace(passCode.at(1))) {
     passCode = passCode.at(0);
   passCode = "_";
  }
cout << "1";

passCode = passCode.at(0); This is an assignment of a character to a string. Actually, I was surprised that this didn't cause a crash. Turns out this is valid, but the result is that passCode is now a string of length 1 with only component 'a'.

Next you immediately execute passCode="_"; This is okay, but now passCode is the different string of length 1, namely "_".

Your specifications were that no output is made unless string passCode is changed. Here's a correct version:
Code:
int main() {
   string passCode;
   passCode = "1 ";
   bool changed = false;
   if (isspace(passCode.at(0))) {
      passCode.at(0) = '_';
      changed = true;
   }
   if (isspace(passCode.at(1))) {
      passCode.at(1) = '_';
      changed = true;
   }
   if (changed) {
      cout << passCode << endl;
   }
   return(0}
}

the thing is that the code i cannot use "bool" whatever you see there i can use only... only isspace() at.() and if statements
 
  • #4
needOfHelpCMath said:
the thing is that the code i cannot use "bool" whatever you see there i can use only... only isspace() at.() and if statements

Anyways THANK YOU so much this is my first time learn C++ with no background of programming thank you a lot appreciate it
 

FAQ: Im i think i am very close....what did i do wrong

What does "Im i think i am very close" mean?

"Im i think i am very close" is not a complete sentence and is therefore difficult to interpret. It could mean that the person is close to reaching a goal or solving a problem, but without more context it is impossible to determine the exact meaning.

What type of mistake did I make if I am close but not quite there?

Without knowing the specific situation or task, it is impossible to determine what mistake may have been made. It could be a simple error in calculation or a misunderstanding of the problem. It is best to review your work and try to identify any potential issues.

Could there be multiple reasons why I am not quite there yet?

Yes, there could be multiple reasons why you are not quite there yet. It could be a combination of small mistakes or a lack of understanding of the problem. It is important to carefully review your work and consider all possibilities before determining the cause.

Is it common to be close to a solution but not quite there?

Yes, it is common to be close to a solution but not quite there. In fact, many scientific breakthroughs come from being on the brink of a solution and then making a small adjustment or realization. The key is to not give up and continue to analyze and experiment until the solution is found.

What steps should I take if I am close but not quite there yet?

If you are close but not quite there yet, it is important to carefully evaluate your work and identify any potential mistakes or misunderstandings. You may also want to seek the advice of a colleague or mentor to gain a fresh perspective. It is also helpful to take breaks and come back to the problem with a clear mind. Persistence and determination are key to finding the solution.

Similar threads

Replies
4
Views
10K
Replies
1
Views
5K
Replies
2
Views
6K
Replies
5
Views
4K
Replies
5
Views
2K
Replies
1
Views
6K
Replies
1
Views
6K
Back
Top