Replacing an integer value with a blank space
Dec 11, 2012 at 12:35am UTC
If the number zero is acquired via ifstream, how can you replace it with a blank space? I've looked around the web and found related things but nothing that helps with my particular issue. I have tried to simply replace the zero with ' ' but it doesn't work and sends my iomanip settings haywire. Relevant part of the program is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
if (!inFile.fail())
{
inFile >> studentID >> lastname >> firstname >> quiz1 >> quiz2 >> quiz3 >> quiz4 >> final;
cout << " Fall 2012 Student Grade Report:" << endl << endl;
cout << "No. ID Last Name First Name Quiz1 Quiz2 Quiz3 Quiz4 Final Avg Grd" << endl;
cout << "-------------------------------------------------------------------------------" << endl;
while (!inFile.eof())
{
cnt++;
avgGrd (quiz1, quiz2, quiz3, quiz4, final, average, grade);
if (quiz1 != 0)
{
q1AVG += quiz1;
q1cnt++;
}
else
cout << ' ' ;
if (quiz2 != 0)
{
q2AVG += quiz2;
q2cnt++;
}
else
cout << ' ' ;
if (quiz3 != 0)
{
q3AVG += quiz3;
q3cnt++;
}
else
cout << ' ' ;
if (quiz4 != 0)
{
q4AVG += quiz4;
q4cnt++;
}
else
cout << ' ' ;
finalAVG += final;
avgAVG += average;
cout << right << setw(3) << cnt << setw (7) << studentID << left << setw (2) << " "
<< setw (15) << lastname << setw (10) << firstname << " "
<< setw (5) << right << quiz1 << setw (6) << quiz2 << setw (6) << quiz3 << setw(6) << quiz4
<< setw (6) << final << setw (8) << average << " " << grade << endl;
inFile >> studentID >> lastname >> firstname >> quiz1 >> quiz2 >> quiz3 >> quiz4 >> final;
}
cout << " --------------------------------------" ;
cout << setw (46) << right << q1AVG / q1cnt << setw (6) << q2AVG / q2cnt << setw (6) << q3AVG / q3cnt
<< setw (6) << q4AVG / q4cnt << setw (6) << finalAVG / cnt << setw (6) << avgAVG / cnt << endl;
inFile.close();
Dec 11, 2012 at 12:52am UTC
You can't put chars in an int
. You could use int
then convert it to char
to have a space.
Dec 11, 2012 at 1:21am UTC
i created char blank = ' ', and for the else statement changed the code to this. Now all the zeros read as 32. Any ideas?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
if (quiz1 != 0)
{
q1AVG += quiz1;
q1cnt++;
}
else
quiz1 = blank;
if (quiz2 != 0)
{
q2AVG += quiz2;
q2cnt++;
}
else
quiz2 = blank;
if (quiz3 != 0)
{
q3AVG += quiz3;
q3cnt++;
}
else
quiz3 = blank;
if (quiz4 != 0)
{
q4AVG += quiz4;
q4cnt++;
}
else
quiz4 = blank;
Dec 11, 2012 at 1:57am UTC
I just noticed in your first version shown you did not use "
for cout
. Try changing that or do this:
Don't set the int as a space with a char.
Example how to do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
int tf = 0;
if (quiz1 != 0)
{
q1AVG += quiz1;
q1cnt++;
}
else
tf = 1;
if (quiz2 != 0)
{
q2AVG += quiz2;
q2cnt++;
}
else
tf = 1;
if (quiz3 != 0)
{
q3AVG += quiz3;
q3cnt++;
}
else
tf = 1;
if (quiz4 != 0)
{
q4AVG += quiz4;
q4cnt++;
}
else
tf = 1;
}
if (tf == 1) cout << " " ;
Never mind this I think I'm wrong.
Last edited on Dec 11, 2012 at 2:02am UTC
Dec 11, 2012 at 2:01am UTC
Never mind that shouldn't be the problem. Tell me what it does when you run it.
Dec 11, 2012 at 9:12pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
while (!inFile.eof())
{
int tf = 0;
cnt++;
avgGrd (quiz1, quiz2, quiz3, quiz4, final, average, grade);
if (quiz1 != 0)
{
q1AVG += quiz1;
q1cnt++;
}
else
tf = 1;
if (quiz2 != 0)
{
q2AVG += quiz2;
q2cnt++;
}
else
tf = 1;
if (quiz3 != 0)
{
q3AVG += quiz3;
q3cnt++;
}
else
tf = 1;
if (quiz4 != 0)
{
q4AVG += quiz4;
q4cnt++;
}
else
tf = 1;
if (tf == 1)
cout << " " ;
finalAVG += final;
avgAVG += average;
cout << right << setw(3) << cnt << setw (7) << studentID << left << setw (2) << " "
<< setw (15) << lastname << setw (10) << firstname << " "
<< setw (5) << right << quiz1 << setw (6) << quiz2 << setw (6) << quiz3 << setw(6) << quiz4
<< setw (6) << final << setw (8) << average << " " << grade << endl;
This creates a space before the entire cout << right << setw... if there is a zero in the inFile
Topic archived. No new replies allowed.