Thread: Need help!

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    10

    Need help!

    Hello I have made a program that outputs a menu where you can do different things, it does kinda what I want it to do put it feel like you could simplify it somehow. I am new to programming so if someone has any good tip I would aprecciate it!

    Here is the code:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    int main()
    {
        int j;
        string proceed;
    
    
        do
        {
             cout << "Here are a list of different choices:\n";
             cout << "\t1. Bajs\n";
             cout << "\t2. Kiss\n";
             cout << "\t3. Neger\n";
             cout << "\t4. Beer song\n";
             cout << "\t0. Quit\n";
    
    
             cout << "\nPlease enter one of the number from the list: ";
             cin >> j;
             cout << '\n';
    
    
             if ( j == 1 )
             {
                 cout << "You are a bajskorv!\n";
             }
    
    
             else if ( j == 2 )
             {
                 cout << "You like to kissa!\n";
             }
    
    
             if ( j == 3 )
             {
                 cout << "You are a black independent woman!\n";
             }
    
    
             else if ( j == 4 )
             {
                 for ( int i = 99; i >= 0; i-- )
                 {
                     if ( i == 1 )
                        {
                            cout << i << " bottle of beer on the wall, " << i << " bottle of beer.\n";
                            cout << "Take one down and pass it around, no more bottles of beer on the wall.\n";
                            continue;
                        }
    
    
                    if ( i != 0 )
                    {
                            cout << i << " bottles of beer on the wall, "  << i << " bottles of beer.\n";
                            cout << "Take one down and pass it around, " << i - 1 << " bottles of beer on the wall.\n";
                            cout << '\n';
                    }
    
    
                    if ( i == 0 )
                    {
                            cout << '\n';
                            cout << "No more bottles of beer on the wall, no more bottles of bear\n";
                            cout << "Go to the store and buy some more, 99 bottles of beer on the wall.\n";
                    }
                }
             }
    
    
             if ( j != 1 && j != 2 && j != 3 && j != 4 && j != 0 )
             {
                 cout << "Not a valid option, please choose a correct number from the list!";
             }
    
    
             if ( j == 0 )
             {
                 return 0;
             }
    
    
             cout << "\n\n";
    
    
             cout << "Do you want to choose another number?\n";
             cout << "y or n: ";
             cin >> proceed;
    
    
             if ( proceed == "n" || proceed == "N" )
             {
                 cout << "Thank you, come again\n";
                 return 0;
             }
             else if ( proceed == "y" || proceed == "Y" )
             {
                 cout << "Here you see the list again:\n\n";
             }
    
    
             if ( proceed != "y" || proceed != "Y" || proceed != "n" || proceed != "N" )
             {
                 int k = 0;
                 while ( k == 0)
                 {
                     cout << "Not an option, try again!\n\n";
                     cout << "Choose one of the following:s\n";
                     cout << "y or n: ";
                     cin >> proceed;
    
    
                     if ( proceed == "n" || proceed == "N" )
                     {
                        cout << "Thank you, come again\n";
                        return 0;
                     }
                     else if ( proceed == "y" || proceed == "Y" )
                     {
                        cout << "Here you see the list again:\n\n";
                     }
                 }
            }
        }
        while ( true );
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    if ( proceed != "y" || proceed != "Y" || proceed != "n" || proceed != "N" )
    This line doesn't make sense it is always true.
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    Yeah i know, I have already changed that

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    now I am really confused, this code doesn't work at all. why?

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int main()
    {
        string password;
        for ( int i = 3; i > 0; i-- )
        {
            cout << "Please enter your password: ";
            getline ( cin, password, 'n' );
    
    
            if ( password != "yyz" )
            {
                cout << "wrong password, " << i - 1 << " tries left!\n";
                return 0;
            }
    
    
            else
            {
                cout << "You actually entered the correct password, welcome to nothing! ";
                i = 3;
            }
        }
    }

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    getline ( cin, password, 'n' );
    Are you entering the character 'n' to end your input? This is not the same as the new line character, which is the default termination character for getline().

    Jim

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also, people generally don't like seeing certain [censored] words. I'd be more considerate with what I type in the future.
    Last edited by Elysia; 04-29-2013 at 04:42 PM.
    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.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    I am sorry for my bad sense of humor, of course I don't want to insult anyone! But I didn't think anyone would recognice swedish words anyway, my bad, sorry!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't you've insulted anyone. Don't worry. I was just giving you a little friendly warning. It's just that, unfortunately, some people do take such things seriously (I'm not one of them, btw).
    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.

  9. #9
    Registered User Inkinsarto's Avatar
    Join Date
    Apr 2013
    Posts
    34
    Try this:
    Code:
    #include <iostream>
    #include <string>
     
     
    using namespace std;
     
     
    int main()
    {
        string std::password;
        for ( int i = 3; i > 0; i-- )
        {
            cout << "Please enter your password: ";
            getline(std::cin, password);
     
     
            if ( password != "yyz" )
            {
                cout << "wrong password, " << i - 1 << " tries left!\n";
                return 0;
            }
     
     
            else
            {
                cout << "You actually entered the correct password, welcome to nothing! ";
                i = 3;
            }
        }
    }
    

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Inkinsarto View Post
    Try this:
    Code:
    #include <iostream>
    #include <string>
     
     
    using namespace std;
     
     
    int main()
    {
        string std::password;
        for ( int i = 3; i > 0; i-- )
        {
            cout << "Please enter your password: ";
            getline(std::cin, password);
     
     
            if ( password != "yyz" )
            {
                cout << "wrong password, " << i - 1 << " tries left!\n";
                return 0;
            }
     
     
            else
            {
                cout << "You actually entered the correct password, welcome to nothing! ";
                i = 3;
            }
        }
    }
    
    I tried, but...
    Code:
    test.cpp: In function 'int main()':
    test.cpp:10:17: error: invalid use of qualified-name 'std::password'
    test.cpp:14:27: error: 'password' was not declared in this scope
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User Inkinsarto's Avatar
    Join Date
    Apr 2013
    Posts
    34
    try including one of these header files:
    Code:
    #include <algorithm>  
    #include <cctype>
    #include <windows.h>
    #include <stdio.h>    
    #include <locale>

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Inkinsarto
    try including one of these header files:
    Sigh. Inkinsarto, when you get an error message, your first reaction should not be to include more header files. Rather, you should read the error message and understand what it says.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Inkinsarto View Post
    try including one of these header files:
    Code:
    #include <stdio.h>
    for C++ it should be <cstdio>
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  14. #14
    Registered User Inkinsarto's Avatar
    Join Date
    Apr 2013
    Posts
    34
    Code:
    test.cpp:14:27: error: 'password'was not declared in thisscope

    password is still declared, but not in that scope. In other words I thought this meant that either 'password' needed to be defined differently,
    or a different header file needed to be added to allow std:: to be used.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Inkinsarto
    password is still declared, but not in that scope. In other words I thought this meant that either 'password' needed to be defined differently,
    or a different header file needed to be added to allow std:: to be used.
    This means that you don't understand the code. Look at this snippet:
    Code:
        string std::password;
        for ( int i = 3; i > 0; i-- )
        {
            cout << "Please enter your password: ";
            getline(std::cin, password);
    password is the second argument to getline. We can reason that this means password was intended to be a std::string variable. As such, this line:
    Code:
    string std::password;
    should have been:
    Code:
    string password;
    In fact, this was what FruixX wrote, and quick check shows that this is the only thing you changed, i.e., you changed the code to make it have an unintended compile error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed