'if(isspace()) statement not working C++
I am working on a function for my program that reads the first and last name from a text file and saves them as two strings. However, I cannot get the if(isspace(next)) statement to execute when the for-loop reaches the first space in between the first and last names.
Here is the full program
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <ctype.h>
using namespace std;
void calcAvg(ifstream& in, ofstream& out);
int main()
{
//open input and output file streams and check for any failures
ifstream input;
ofstream output;
input.open("lab08_in.txt");
output.open("lab08_out.txt");
if(input.fail())
{
cout << "Error: File not Found!" << endl;
exit(1);
}
if(output.fail())
{
cout << "Error: File creation failed!" << endl;
exit(1);
}
calcAvg(input, output);
return 0;
}
void calcAvg(ifstream& in, ofstream& out)
{
int sum = 0;
double average;
//save first and last name to strings
string firstname, lastname;
char next;
int i = 1;
in >> next;
for(; isalpha(next) && i < 3; in >> next)
{
if(i == 1)
{
firstname += next;
cout << next << " was added to firstname" << endl;
if(isspace(next))
{
cout << "Space!" << endl;
out << firstname << ' ';
i++;
}
}
else if(i == 2)
{
lastname += next;
cout << next << " was added to lastname" << endl;
if(isspace(next))
{
cout << "Space!" << endl;
out << lastname << ' ';
i++;
}
}
}
}
The section of code that I am having troubles with is
if(isspace(next))
{
cout << "Space!" << endl;
out << firstname << ' ';
i++;
}
The code is supposed to (in my mind) read each character from the file and add to the string and once it reaches a space write that string firstname to the output file, but it doesn't, instead I get this output in the console
H was added to firstname
e was added to firstname
s was added to firstname
s was added to firstname
D was added to firstname
a was added to firstname
m was added to firstname
etc...
Notice how the name is supposed to be Hess Dam.... and what is supposed to happen is it saves Hess to firstname and Dam... to lastname. Instead it is just adding the whole thing up until the tab after the last name in the firstname string and it never writes to the output file. It reads the tab because it exits the for loop (from the isalpha(next)) but the isspace(next) argument doesn't work for some reason
Solution 1:[1]
Sorry, does not have enought reputation to comment it, but there are two wrong answers. The comment from zahir is right. std::isspace(c,is.getloc()) is true for the next character c in is (this whitespace character remains in the input stream). Operator >> will never return whitespaces.
Solution 2:[2]
You are checking if next is an alpha-character in the for-loop: for(; isalpha(next) && i < 3; in >> next).
According the documentation, a "space" character is not considered an alpha-character in the default C locale. You could either change your locale to get around this, or, more preferably (in my opinion), modify the for-loop to accept spaces as well.
Something like for(; (isalpha(next) || isspace(next)) && i < 3; in >> next) should allow the loop to process the space along with the alpha-characters
EDIT: As several others have pointed out, I missed the fact that using the >> operator here will cause you to never see the whitespace in the first place, so my answer is not complete. I'm going to leave it just in case it can still prove useful.
Solution 3:[3]
By default, operator>> will ignore all whitespace characters, but you can use the manipulator noskipws to circumvent this behavior like so:
in >> noskipws >> next;
Solution 4:[4]
Your issue is not with isspace function, it's with your for loop, you're forcing your for loop to process only alphabetical characters by using isalpha, in this case, the character cannot be iscntrl, isdigit, ispunct or isspace). Take a look at this.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Philip Lindholm |
| Solution 4 | Mo Abdul-Hameed |
