CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2009
    Posts
    94

    Question 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!

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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 ?

  3. #3
    Join Date
    Mar 2009
    Posts
    94

    Re: c++ Primer exercise 6.8 (switch-statement)

    thank you. I didnt think of that.
    If my input is e.g.
    Code:
    a b c
    it ignores the white spaces and checks only for abc. My guess is, it has something to do with char, but I am clueless...

  4. #4
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    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

  5. #5
    Join Date
    Mar 2009
    Posts
    94

    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
  •  





Click Here to Expand Forum to Full Width

Featured