Like Tree1Likes
  • 1 Post By kmdv

Jumping into C++

This is a discussion on Jumping into C++ within the C++ Programming forums, part of the General Programming Boards category; Is there any way to check whether the user has inputed an integer or string, for example I want to ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    114

    Jumping into C++

    Is there any way to check whether the user has inputed an integer or string, for example I want to tell the user when he has entered a letter in the following program which adds the numbers with previous,
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {
        int x;
        cin>>x;
        int y;
        while (true){
    
    
        cin>>y;
    
    
        cout<<x+y;
    
    
    x=x+y;
    
    
    
    
    
    
    
    
        }
    }
    that "invalid input,please only insert numbers" ? it seems when I enter letter as input it forms an infinite loop.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    Read the site's tutorials if the book really doesn't go into it: A Gentle Introduction to IO Streams in C++ - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Quote Originally Posted by whiteflags View Post
    Read the site's tutorials if the book really doesn't go into it: A Gentle Introduction to IO Streams in C++ - Cprogramming.com
    I couldnt find what I was looking for, I wanted to check whether the user has inputted an integer or string and reply accordingly..

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by Tamim Ad Dari View Post
    I couldnt find what I was looking for, I wanted to check whether the user has inputted an integer or string and reply accordingly..
    You have to specify exactly what is a string and what is an integer. If string is everything that is not an integer, first attempt to read input as an integer, and if that fails, read it as a string.

    And please, start giving your threads more meaningful names.
    stahta01 likes this.
    I never put signature, but I decided to make an exception.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Tamim Ad Dari View Post
    I couldnt find what I was looking for, I wanted to check whether the user has inputted an integer or string and reply accordingly..
    Look at that link where it says "Error handling with IO streams"

    You can also just read the user input into a string and then handle the input by checking first if it is a string. This separates the problem into first handling I/O and then interpreting I/O.

    Code:
    std::string input;
    //get the input first
    std::getline(std::cin, input);
    //then use string stream to convert the input to number or any other method
    int number;
    std::stringstream sstr(input);
    if (sstr >> input) {
       //input contains the first integer the user inputted before pressing enter
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping into C++
    By Tamim Ad Dari in forum C++ Programming
    Replies: 9
    Last Post: 01-11-2013, 08:45 AM
  2. Jumping into C++ problems
    By Tamim Ad Dari in forum C++ Programming
    Replies: 8
    Last Post: 01-11-2013, 08:04 AM
  3. Jumping to OOP in C++ from C
    By meadhikari in forum C++ Programming
    Replies: 6
    Last Post: 07-16-2010, 05:26 AM
  4. Jumping in the code...
    By AdampqmabA in forum C++ Programming
    Replies: 24
    Last Post: 10-06-2008, 05:34 PM
  5. Jumping between functions
    By Nexus-ZERO in forum C Programming
    Replies: 8
    Last Post: 01-14-2008, 02:47 AM

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