Remove Space, Stuck on lab

Joined
Nov 6, 2022
Messages
2
Reaction score
0
Hello, I'm currently taking a C++ course and the last few weeks have been a complete headache. I have this lab where i'm suppose to remove spaces and I keep getting errors with my code. I have no idea what I'm doing wrong. Can anyone spot my error?

#include <iostream>
using namespace std;

int main() {

string userInput;
getline(cin, userInput);

for (int i = 0; i < userInput.length(); ++i){
if (isspace(userInput.at(i))){
userInput.at(i) = "";
}
}

cout<< userInput <<endl;

return 0;
}

I also tried another code but received the same error

#include <iostream>
using namespace std;

int main() {

string userInput;
getline(cin, userInput);

for (int i = 0; i < userInput.length(); ++i){
if (userInput.at(i) = " "){
userInput.at(i) = "";
}
}

cout<< userInput <<endl;

return 0;
}
 
Joined
Nov 6, 2022
Messages
2
Reaction score
0
Never mind, after hours of digging through the web it looks like the problem was the double quotes. o_O I guess the comparison was therefore between a char and a string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,056
Messages
2,570,440
Members
47,101
Latest member
DoloresHol

Latest Threads

Top