Loops

This is a discussion on Loops within the C++ Programming forums, part of the General Programming Boards category; I'm trying to create a code that will ask the user for a number and then perform some calculations based ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    8

    Loops

    I'm trying to create a code that will ask the user for a number and then perform some calculations based on that number. What I want the program to do is:
    If I type a character, the program will display a message and then take me back to the beginning of the program, where I can input again.
    If I type a number the program is to carry on normally.
    I assume I have to use loops for this but I have no idea how to do that. Could anyone help me out?

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    yeah, the easiest way would be to use a loop, here is a small example of what you want to do:

    (its pseudocode by the way)

    int main ( void )
    {
    loop ( ) ...
    {
    cin >> char_which_was_inputted;
    if ( isalpha ( char_which_was_inputted ) )
    display message
    else if ( isdigit ( char_which_was_inputted ) )
    break; //breaks out of the loop
    }

    ...rest of program goes here...

    return 0;
    }
    My Website

    "Circular logic is good because it is."

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    That might be a little tricky, because it involves determining what data type you entered, and I don't think that's possible (if it is, I need to do that in a C++ decoding program I'm making, so please tell me if you can!). You could convert them to integers to get the ASCII value, and then if it's between 97 and 122, it's a lowercase letter, if it's between 49 and 57, it's a number, and if it's between 65 and 90, it an uppercase letter. Use ifs for that, and if it's a number, use a switch statement to assign the value to an int, which you can't do directly since it's a char at this point. Good luck.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    8
    Let's just say I'm pretty confused right now
    I'll take you advice but I don't think I have the skills to do something like that yet...

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    30
    This bit of code will accept one character. If it is a not a number, it will loop back to the start, if it is a number, it will convert it to an int (from a char) and display it.

    If you need more explanation or help then post here.

    Code:
    /*                 Title of project                    
                       By P Grundy                     
                       Date etc                         */                      
    
    
    
    
    /*                     Header files                 */
    /*                     ---------------              */
    #include <iostream.h> //for io
    
    
    
    /*                     Main Program                 */
    /*                     ------------                 */
    int main()
    {
    
    	char digit;
    	int number;
    
    	do
    	{
    
    	cout<<"Enter a number or character\t";
    	cin>>digit;
    
    	}
    	while(digit<'0'||digit>'9');
    
    	number = digit-48;
    	cout<<"Your number is "<<number<<"\n";
    
    
    	return 0;
    }
    Homer

    D'OH!

    mmmmmmmm... iterations

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    You can use iostreams built in type checking -

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	
    	int a;
    	cin >> a;
    	while(cin.fail()){
    		cin.clear();
    		cin.ignore();
    		cin >> a;
    	}
    
    	cout << a;
        return 0;
    }
    The stream will be put in a fail state if it doesn't extract something that can be converted into an interger, whereby you can clear it and have another go at obtaining input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple thread for loops
    By lehe in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2009, 12:01 PM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 12:18 AM
  3. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 11:09 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM

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