trying to get the computer to count user input digits

This is a discussion on trying to get the computer to count user input digits within the C++ Programming forums, part of the General Programming Boards category; I'm just testing what I've wrote so far to check if it's correct, but it's not...where did I go wrong? ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    33

    trying to get the computer to count user input digits

    I'm just testing what I've wrote so far to check if it's correct, but it's not...where did I go wrong?


    Code:
    /* 
       Purpose: This program accepts a number, up to 5-digits, and the prints it out
       in English.
    */
    #include<iostream.h>
    
    int main()
    {
    	//User's inputs, stored in char, so whitespace characters can be evaluated
    	char char1;
    	char char2;
    	char char3;
    	char char4;
    	char char5;
        //variable that will count the digits the user will input, intialized to 0
    	int digits;
    	digits=0;
        
    	//Program Description and prompt to user
    	cout<<"This program accepts a number, up to 5-digits and then prints it out\n"
    		<<"in English.  Please enter in a 5-digit or less number:  ";
    	
    	//Getting user's data
    	cin>>char1;
    	cin.get(char2);
    	cin.get(char3);
    	cin.get(char4);
    	cin.get(char5);
        
    	if(char5==' ')
    		digits++;
    	if(char4==' ')
    		digits++;
    	if(char3==' ')
    		digits++;
    	if(char2==' ')
    		digits++;
    	if(char1==' ')
    		digits++;
    	
    	//trying to get digits to print out, why doesn't it work?
    	cout<<endl<<endl<<digits;
    
    
    	return 0;
    }
    Last edited by m712; 11-17-2002 at 03:27 PM.

  2. #2
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    I think using a string would be the best way

    Code:
    #include<string>
    
    int main(){
    
    
    string s;
    
    cout<< "enter whatever:";
    
    cin >> s;
    
    cout << "you entered " << s.size();
    
    
    return 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    33
    Thanks. That should work great. I think the problem with my code above is that if the user only enters 3 numbers char4 and char5 will not even be read--not even as blankspace characters, so digits will always be equal to 0. I will try your method; it sounds much better.

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    You say digits, but you use syntax for strings- If you use numbers you can use / and %.
    Mr. C: Author and Instructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. timed user input
    By sainiabhishek in forum C Programming
    Replies: 4
    Last Post: 04-01-2009, 11:59 AM
  2. Truncating user input
    By CS_Student8337 in forum C Programming
    Replies: 10
    Last Post: 03-19-2009, 12:34 AM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 02:53 PM
  4. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 09:53 AM
  5. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21