[C++] LF help. menu

Shout-Out

User Tag List

Results 1 to 12 of 12
  1. #1
    P1raten's Avatar Banned
    Reputation
    500
    Join Date
    Mar 2008
    Posts
    1,323
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [C++] LF help.

    I need a little help. When i type 10 it just says 1 when it should say 10.

    Code:
    //Main
    
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
     
    int main()
    {
        char choice;
        for (;;){
         do {
        cout<<"\nChoose a number between 1-20.\n";
    	cout<<"\n";
        cin>>choice;
    	 } while ( choice < '1' || choice > '10' );
        switch (choice) {
               case '1':
    				cout<<"\n";
    				cout<<"1";
    				cout<<"\n";
                    break;
               case '2':
    				cout<<"\n";
    				cout<<"2";
    				cout<<"\n";
                    break;
               case '3':
    				cout<<"\n";
    				cout<<"3";
    				cout<<"\n";
                    break;
               case '4':
    				cout<<"\n";
    				cout<<"4";
    				cout<<"\n";
                    break;
    		   case '5':
    				cout<<"\n";
    				cout<<"5";
    				cout<<"\n";
                    break;
    		   case '6':
    				cout<<"\n";
    				cout<<"6";
    				cout<<"\n";
                    break;
    		   case '7':
    				cout<<"\n";
    				cout<<"7";
    				cout<<"\n";
                    break;
    		   case '8':
    				cout<<"\n";
    				cout<<"8";
    				cout<<"\n";
                    break;
    		   case '9':
    				cout<<"\n";
    				cout<<"9";
    				cout<<"\n";
                    break;
    		   case '10':
    				cout<<"\n";
    				cout<<"10";
    				cout<<"\n";
                    break;
               default:
                        cout<<"That is not an option";
                        
                    }
     
    }
    	cout<<"\n";
    return 0;
    
    
    }
    
    int stuff() {
    	return 0;
    }
    FlameShield ON

    [C++] LF help.
  2. #2
    Cromon's Avatar Legendary


    Reputation
    840
    Join Date
    Mar 2008
    Posts
    714
    Thanks G/R
    0/7
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You read a char from the inputqueue. If you enter 10 to the queue the first character is 1 and the second 0. Therefore if you request one character from the queue you get 1. The better solution would be to request a whole line using std::cin.getline(params) and then try to convert it to an integer using sscanf (better than atoi cause it allows you to check if the text could not be converted while atoi only returns 0 on failure).

  3. #3
    piller's Avatar Contributor
    Reputation
    82
    Join Date
    May 2007
    Posts
    103
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  4. #4
    P1raten's Avatar Banned
    Reputation
    500
    Join Date
    Mar 2008
    Posts
    1,323
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Cromon View Post
    You read a char from the inputqueue. If you enter 10 to the queue the first character is 1 and the second 0. Therefore if you request one character from the queue you get 1. The better solution would be to request a whole line using std::cin.getline(params) and then try to convert it to an integer using sscanf (better than atoi cause it allows you to check if the text could not be converted while atoi only returns 0 on failure).
    Originally Posted by piller View Post
    Just so you know; im a total n00b at C++.

  5. #5
    piller's Avatar Contributor
    Reputation
    82
    Join Date
    May 2007
    Posts
    103
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I´m not i c++ either. i´m delphian (shame on me)

    would try something like this

    Code:
    //Main
    
    #include <cstdlib>
    #include <iostream>
    #include <stdio.h>
    
    using namespace std;
     
    int main()
    {
        char choice;
        char str [2];
        int i;
    
    ////////////////////////////////////////////
    
        sscanf (choice,"%d",str,&i); <-- here you get your input to integer i guess
    
    and you can now go along with you code:
    
    while ( choice < '1' || choice > '10' );
        switch (choice) {
               case '1':
    				cout<<"\n";
    				cout<<"1";
    				cout<<"\n";
                    break;
               case '2':
                            ...
    but maybe im totally WRONG sir

  6. #6
    Cromon's Avatar Legendary


    Reputation
    840
    Join Date
    Mar 2008
    Posts
    714
    Thanks G/R
    0/7
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, lets imagine you only allow 2-digit inputs then we use the following:
    Code:
    #include <iostream>
    
    int main()
    {
    	char szInput[3] = { 0 };
    	int iConverted = 0;
    
    	std::cin.getline(szInput, 3);
    
    	if(sscanf(szInput, "%i", &iConverted) != 1)
    	{
    		std::cout << "You did not enter a number!" << std::endl;
    		return 0;
    	}
    
    	switch(iConverted)
    	{
    	case 0:
    		std::cout << "0" << std::endl;
    		break;
    	case 1:
    		std::cout << "1" << std::endl;
    		break;
    	}
    
    	return 0;
    }

  7. #7
    BonutDot's Avatar Contributor
    Reputation
    235
    Join Date
    Aug 2006
    Posts
    418
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alternatively, define "choice" as some sort of numerical value, an int perhaps.

    EDIT: Yeah cromon is right. Char is a bad idea all around for what you're doing.
    Last edited by BonutDot; 12-15-2009 at 01:18 PM.

  8. #8
    Cromon's Avatar Legendary


    Reputation
    840
    Join Date
    Mar 2008
    Posts
    714
    Thanks G/R
    0/7
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats not really good. Still choice is a char which means _1_ character gets read by cin. So this will still be the 1 and _not_ 10 because 10 are 2 digits!

    Also the switch cannot work because '10' is 0x3130 and because the maximum of char is 0xFF it will in no case be 0x3130.

  9. #9
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And this thread is a testament to why mixing C and C++ is a bad idea.

    The STL string class is there for a reason. Use it.
    The STL stringsteam class is there for a reason. Use it.

    Take input as a string, and use a stringstream to convert it to the target type.

    If you're feeling lazy or you already have a Boost dependency, then Boost.LexicalCast is a quick and easy way to do the above without having to write the boiler-plate code.

    This is assuming of course that you want to do the conversion, otherwise why not just take input directly as the target type?
    Last edited by Cypher; 12-19-2009 at 11:12 PM.

  10. #10
    hexonflux's Avatar Member
    Reputation
    10
    Join Date
    Mar 2007
    Posts
    40
    Thanks G/R
    1/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What? Yea mixing C and C++ is only really called for when you learn
    C first and move into C++. It just becomes habit. As far as using
    stringstream and adding a lot of extra crap into the application, just
    do it the simple way. Trying to over complicate things leads to major
    mistakes and heavy runtime.

    I've taken your code and made it into what I favored to be
    what you were trying to do. I hope it helps.



    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    
    
    int main()
    {
        short choice = 0;
    
        while( choice >= 1 || choice <= 10 )
        {
            cout << "\nChoose a number between 1-10: ";
            cin >> choice;
            cout << "\n";
    
            switch( choice )
            {
                case 1:
                    cout << 1;
                    break;
    
                case 2:
                    cout << 2;
                    break;
    
                case 3:
                    cout << 3;
                    break;
    
                case 4:
                    cout << 4;
                    break;
    
                case 5:
                    cout << 5;
                    break;
    
                case 6:
                    cout << 6;
                    break;
    
                case 7:
                    cout << 7;
                    break;
    
                case 8:
                    cout << 8;
                    break;
    
                case 9:
                    cout << 9;
                    break;
    
                case 10:
                    cout << 10;
                    break;
    
                default:
                    cout << "That is not an option.";
            }
            cout << "\n";
        }
    
        return 0;
    }
    - Hex

  11. #11
    Cromon's Avatar Legendary


    Reputation
    840
    Join Date
    Mar 2008
    Posts
    714
    Thanks G/R
    0/7
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Cypher View Post
    And this thread is a testament to why mixing C and C++ is a bad idea.
    Not at all! It is not "a bad idea" its just risky and you can very simple shoot yourself but on the other hand it can reduce not needed overhead!

  12. #12
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Cromon View Post
    Not at all! It is not "a bad idea" its just risky and you can very simple shoot yourself but on the other hand it can reduce not needed overhead!
    No. For the purposes C++ is used for in this context, in 99.99% of cases it is a BAD IDEA.

    Very few programs are CPU bound and hence there is usually very little 'overhead' to 'reduce'. Yes, it's always good to avoid premature pessimization. But premature optimization is worse.

    It's a lot easier to make a correct program fast than it is to make a fast program correct.

Similar Threads

  1. Help WoW Fish-Bot
    By Eliteplague in forum World of Warcraft General
    Replies: 2
    Last Post: 12-10-2024, 05:46 PM
  2. HELP: Gold Scam Exploit
    By GoldDragon in forum World of Warcraft General
    Replies: 11
    Last Post: 01-23-2007, 07:26 PM
  3. Banner Ad Redesign help
    By Matt in forum Community Chat
    Replies: 57
    Last Post: 07-08-2006, 08:40 PM
  4. Hit points and talent points? Please help
    By hankusdankus in forum World of Warcraft General
    Replies: 6
    Last Post: 05-04-2006, 02:00 PM
  5. bot help
    By xwhitedeathx in forum World of Warcraft General
    Replies: 3
    Last Post: 05-01-2006, 03:50 AM
All times are GMT -5. The time now is 01:58 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search