-
March 23rd, 2009, 01:59 PM
#1
c++ Primer exercise 6.8 (switch-statement)
Hello everyone,
I am trying to learn c++ with the c++ Primer and I do have a problem with exercise 6.8:
"Modify our vowel-count program so that it also counts the number of blank spaces, tabs, and newlines read."
My solution doesnt count them and I dont know why...
This is my code:
Code:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int a = 0;
int e = 0;
int i = 0;
int o = 0;
int u = 0;
int space = 0;
int tab = 0;
int nl = 0;
char eingabe;
while (cin >> eingabe)
{
switch (eingabe)
{
case '\n':
++nl;
break;
case '\t':
++tab;
break;
case ' ':
++space;
break;
case 'a': case 'A':
++a;
break;
case 'e': case 'E':
++e;
break;
case 'i': case 'I':
++i;
break;
case 'o': case 'O':
++o;
break;
case 'u': case 'U':
++u;
break;
default:
;
}
}
cout << a << " as" << endl;
cout << e << " es" << endl;
cout << i << " is" << endl;
cout << o << " os" << endl;
cout << u << " us" << endl;
cout << space << " spaces" << endl;
cout << tab << " tabs" << endl;
cout << nl << " new lines" << endl;
return 0;
}
Can someone please tell me what I am doing wrong here?
Thank you!
-
March 23rd, 2009, 02:05 PM
#2
Re: c++ Primer exercise 6.8 (switch-statement)
My solution doesnt count them and I dont know why...
What do you see happening when you use the debugger to step through it ?
-
March 23rd, 2009, 04:11 PM
#3
Re: c++ Primer exercise 6.8 (switch-statement)
thank you. I didnt think of that.
If my input is e.g.
it ignores the white spaces and checks only for abc. My guess is, it has something to do with char, but I am clueless...
-
March 23rd, 2009, 04:23 PM
#4
Re: c++ Primer exercise 6.8 (switch-statement)
By default whitespace is ignored by input streams.
But there is the iomanipulator noskipws that will do the trick.
Just #include <iomanip>
and add
Code:
cin >> std::noskipws;
Kurt
-
March 24th, 2009, 04:51 AM
#5
Re: c++ Primer exercise 6.8 (switch-statement)
Thank you Kurt! You got me on the right track.
But I wanted to do it by means already covered in the book, so I used getline():
Code:
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
int a = 0;
int e = 0;
int i = 0;
int o = 0;
int u = 0;
int space = 0;
int tab = 0;
int nl = 0;
string line;
while (getline(cin, line))
{
for (string::size_type ix = 0; ix != line.size(); ++ix)
{
switch (line[ix])
{
case '\t':
++tab;
break;
case ' ':
++space;
break;
case 'a': case 'A':
++a;
break;
case 'e': case 'E':
++e;
break;
case 'i': case 'I':
++i;
break;
case 'o': case 'O':
++o;
break;
case 'u': case 'U':
++u;
break;
default:
;
}
}
++nl;
}
cout << a << " as" << endl;
cout << e << " es" << endl;
cout << i << " is" << endl;
cout << o << " os" << endl;
cout << u << " us" << endl;
cout << space << " spaces" << endl;
cout << tab << " tabs" << endl;
cout << nl << " new lines" << endl;
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|