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

  • Context: MHB 
  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a C++ programming problem where participants are trying to modify a string by replacing spaces with underscores in a two-character string called passCode. The focus is on debugging the provided code and adhering to specific constraints regarding the use of programming constructs.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant presents a code snippet that attempts to replace spaces in the string but produces incorrect output, indicating a misunderstanding of string manipulation in C++.
  • Another participant identifies issues in the original code, explaining that assigning a character to a string results in a string of length 1, which is not intended.
  • There is a suggestion for a corrected version of the code that uses a boolean flag to track changes, but it is noted that the original poster cannot use boolean variables.
  • The original poster expresses constraints on their coding capabilities, stating they can only use isspace(), at(), and if statements.
  • The original poster expresses gratitude for the assistance, highlighting their beginner status in learning C++.

Areas of Agreement / Disagreement

Participants generally agree on the problems with the original code and the need for corrections, but there is a disagreement regarding the constraints on the programming constructs that can be used.

Contextual Notes

The original poster's constraints limit the use of certain programming constructs, which may affect the proposed solutions and discussions.

Who May Find This Useful

Individuals learning C++ programming, particularly those with no prior programming background, may find this discussion helpful for understanding string manipulation and debugging techniques.

needOfHelpCMath
Messages
70
Reaction score
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
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}
}
 
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
 
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
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
10K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 2 ·
Replies
2
Views
7K
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 1 ·
Replies
1
Views
6K
Replies
14
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K