Thread: Atoi and such

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105

    Atoi and such

    I made a certain program and I am now trying to get rid of some annoying little bugs. Going to post a part of the code so you guys can help me out .

    Code:
    #include <iostream
    
    int main()
    {
        cout<<"1- New Game" <<endl;
        cout<<"2- Exit" <<endl;
        cout<<"Choose your option: " <<endl;
    
        bool ValidOption = false;
    
    
        while (ValidOption != true)
        {
    
           char option[100];
           cin.clear();
           cin>>option;
    
           int int_option = atoi(option);
    
           switch (int_option)
           {
           case 1:
               ValidOption = true;
               cout<<"ok.";
           case 2:
               ValidOption = true;
               cout<<"ok";
           default:
               ValidOption = false;
               cout<<"Error: ";
               break;
          }
       }
    }
    Ok so the code above displays a simple menu and it will display "ok" whether you insert "1" or "2".
    Now if you are to insert a char, string or anything else it will work, unless you separate it by a space. The loop will run twice considering cin stops at spaces (Which means it will display "error:" twice). Can anyone help me?

    Example:

    1- New Game
    2- Exit
    Choose your option: 3
    Error: 4
    Error: gfgdg
    Error: ad
    Error:^Z
    Error:adsad adada
    Error: Error: 1
    "ok"

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You forgot the break statement on each of your case statements.


    Jim

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >> char option[100];
    >> cin.clear();
    >> cin>>option;
    This is absolutely unacceptable code. Never use cin with a char array. Use std::string instead. This way, you avoid buffer overruns.

    >> int int_option = atoi(option);
    Prefer the C++ way:
    Code:
    int int_option;
    std::stringstream extractor(option);
    if (!(extractor >> int_option))
    	/* Extraction failed; handle it. */ ;
    // Extraction successful.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    I only started manipulating atoi today so I'm not exacly sure as to how it works.

    My first thought was to define the array as string (string option[100]) and convert the string into an integer, yet atoi did not seem to work that way:
    Code:
           string option[100];
           cin.clear();
           getline(cin,option[100]);
     
           int int_option = atoi(option);
    > int int_option = atoi(option);
    Invalid code right here because "std::string is incompatible with parameter of type const char".

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Try:
    Code:
    std::string option;
    std::cin.clear();
    std::getline(std::cin, option);
    
    int int_option;
    std::stringstream extractor(option);
    extractor >> int_option;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Is there something else I should include? Because

    >std::stringstream extractor(option);
    Error:Incomplete type is not allowed (extractor is underlined)

    >extractor >> int_option;
    Error:no operator matches these operands; (>> underlined)

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes.
    <sstream> for stringstream; see std::basic_stringstream - cppreference.com (the link stringstream redirects there).
    See specifically the small text "defined in header".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Elysia View Post
    Prefer the C++ way:
    Code:
    int int_option;
    std::stringstream extractor(option);
    if (!(extractor >> int_option))
    	/* Extraction failed; handle it. */ ;
    // Extraction successful.
    Prefer the C++11 way:

    Code:
    MyString = std::to_string(option);
    Also seems like boost::lexical_cast is proposed for TR2...finally!
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Neo1 View Post
    Prefer the C++11 way:

    Code:
    MyString = std::to_string(option);
    except the OP is actually trying to go the other way, so elysia's method is correct, in the absence of boost::lexical_cast.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Or he could use the stoi() function instead of atoi().

    Jim

  11. #11
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Quote Originally Posted by Elysia View Post
    Try:
    Code:
    std::string option;
    std::cin.clear();
    std::getline(std::cin, option);
    
    int int_option;
    std::stringstream extractor(option);
    extractor >> int_option;
    Exacly what I was looking for. Dem bugs are all gone. tyvm.

    @Jim: Your solution makes sense as well, ty.

  12. #12
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Considering I might use it with some frequence I would like to know what these lines of code do exacly:

    > std::stringstream extractor(option);

    > extractor >> int_option;

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Did you try to Google std::stringstream? This is a class that associates the string (option) with a stream (extractor) once this stream is created you use it just as you would cout or cin.

    Jim

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    std::basic_stringstream - cppreference.com
    Good practice reading documentation. It can be tricky, though, so if you don't understand, then ask, and we shall elaborate.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. use of atoi.
    By mgracecar in forum C Programming
    Replies: 3
    Last Post: 03-06-2012, 09:02 PM
  2. Atoi
    By r_james14 in forum C Programming
    Replies: 2
    Last Post: 02-29-2012, 11:19 AM
  3. Atoi()
    By Stuart Dickson in forum C++ Programming
    Replies: 4
    Last Post: 07-19-2010, 09:16 AM
  4. C++.NET Can I use atoi ??
    By Swaine777 in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2004, 06:54 PM
  5. like atoi
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 09:42 AM