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

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
AI Thread Summary
The discussion centers on a C++ program designed to replace spaces in a 2-character string `passCode` with underscores. The original code contains logical errors, such as incorrectly assigning characters to the string and printing output regardless of whether changes were made. A corrected version is proposed, which checks each character for spaces and replaces them with underscores only if changes occur, ensuring no output is printed if the string remains unchanged. However, the user specifies constraints that limit the use of certain programming constructs, such as boolean variables, and seeks guidance on achieving the desired functionality using only `isspace()`, `at()`, and `if` statements. The user expresses gratitude for the assistance as they are new to programming in C++.
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
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...

Similar threads

Back
Top