Like Tree1Likes

got a problem

This is a discussion on got a problem within the C++ Programming forums, part of the General Programming Boards category; hey i need help i got the book jumping into c++ and it asks me to do this: Write a ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    6

    Question got a problem

    hey i need help i got the book jumping into c++ and it asks me to do this:
    Write a menu program that lets the user select from a list of options, and if the input is not one
    of the options, reprint the list.
    but im stuck, i've already tried a lot of stuff but i cant spot my mistake can someone help me please.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Post your code(your try) and we will help you

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    6
    this is one of the codes i made: #include <iostream>
    Code:
    #include <string>
    using namespace std;
    
    
    int main()
    {
    string option;
    cout << "type an option\n";
    cout << " awesome\n ";
    cout << "nice\n";
    cin >> option;
    while ( option != "awesome" || option != "nice" )
    {
    cout << " type an option\n ";
    cout << " awesome\n ";
    cout << "nice\n";
    cin >> option;
    }
    }
    i don't eaven know wich loop type i have to use.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    while ( option != "awesome" || option != "nice" )
    should be
    Code:
    while ( option != "awesome" && option != "nice" )
    option cannot be "awesome" and "nice" at the same time.

    Kurt
    Cesar Vega likes this.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    6
    thank you, i ´ve spent already like 1 hour trying to make it work!

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Which loop type should you use?You have to think first if you know how from before how many times the loop body is going to be executed.If you know a for type loop is ideal.If not you should use a while or a do-while loop.At your example can you predict accurate how many times the loop is going to be executed?Do you know how many times a user(might be a user that has limitted experience with computers,so making spelling errors all the time) is going to input something that is not an option?If yes you should use a for type loop.If not you should use a do - while or a while loop type.But which one?You should thing if the body of the loop(the menu of the options) is going to be executed at least one time no matter what user wants to input.If you decide that the body must be executed at least one time no matter what,then a do-while is ideal.Else you use a while loop type.So which type are you going to use?

    Then about the condition of the loop.
    You want the menu to be printed if the user inputs something that is not an option.So you have to reprint the list if the option is not awesome AND is not nice.So at line 12 maybe the operators || should be replaced by something else.But by what operators?

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Quote Originally Posted by ZuK View Post
    [CODE]
    option cannot be "awesome" and "nice" at the same time.
    Kurt
    Zuk answered the one question for you,but i suggest you try to answer the first question of mine

  8. #8
    Registered User
    Join Date
    Aug 2012
    Posts
    6
    hey i dont understand what´re you asking me. But well if you could help me once more i´d be thankfull.
    How can i repeat an action just certain times in the for loop.
    i want to create a password checker but i just want it to execute certain times. I'm trying with for but i cant, my code its like this:
    Code:
    #include <iostream>
    //Write a password prompt that gives a user only a certain number of password entry attempts—
    //so that the user cannot easily write a password cracker
    using namespace std;
    
    
    int main()
    {
     for ( int password; password == "pass";    )
    }
    and i have no damn idea of how to make the password repeat just certain times. I think that in the white space should be what i need but im stuck, thank you.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    Just so you know, passwords are easily implemented poorly. Basically, if you compare the password in the program, it's not secure.

    This doesn't need to be secure though.
    Code:
    for ( int password; password == "pass";    )
    and i have no damn idea of how to make the password repeat just certain times.
    Well you have to define "certain times". If you want to give the user 3 tries before locking them out, you can use the for loop to count. Put the code that actually checks the password in the loop body between the braces. If the user gets the password right, you can "break;" out of the loop early and do other stuff after the loop. If the user fails, you will have to close the program after the loop.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    This is a very confused line of code:

    Code:
     for ( int password; password == "pass";    )
    • You've made password an int, then you're trying to compare it to a string. I think you'll need a separate int to track how many attempts have been made
    • You can't compare 2 strings with == in C. You need to use strcmp


    I'm not sure what you mean by
    Quote Originally Posted by Cesar Vega
    I think that in the white space should be what i need
    Anyway. Let's say you want to allow the user to try to enter the password 5 times. You need to stop prompting the user if their tries are all gone or if they have put the password in correctly. Those are your loop exit conditions.

    There are a number of ways you could do this, you could try something like:
    Code:
    for (int tries = 0; (tries < 5) && strcmp(password,"pass") != 0; tries++)
    {
        // read password
    }

  11. #11
    Registered User
    Join Date
    Aug 2012
    Posts
    6
    Hey. smokey, the code doesn´t work, maybe the mistake is mine, i did this:
    [code]
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    string password;
    cin >> password;
    for (int tries = 0; (tries < 5) && strcmp (password,"pass") != 0; tries++)
    {
    // read password
    cout << "acces allowed\n";
    }
    }
    [\code]
    it doesn't work. i'm confused.
    P.S thank you all, im just a noob.
    Last edited by Cesar Vega; 08-10-2012 at 09:44 PM.

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    Well it actually looks pretty OK, but you would want something more like

    Code:
    string password;
    cout << "Enter password: ";
    cin >> password;
    for (int tries = 1; tries < 5; tries++)
    {
       if (password == "pass")
       {
           break;
       }
       else
       {
          cout << "Enter password: ";
          cin >> password;
       }
    }
    
    if (password != "pass")
    {
       quit();
    }
    // secrets beyond this point

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Whoops, sorry, I forgot this was the C++ forum not the C forum. You can use "==" for comparing strings. Sorry about that misinformation.

    The for() loop I suggested was for repeatedly reading password from the user. I think it might be better to go simpler with this instead of cramming a lot into the for loop.

    Code:
    for (int tries = 1; (tries < 5); tries++)
    {
    	if (password == "pass") 
    	{
    		cout << "acces allowed\n";
    		break; //  exit the loop, password was correct so don't want to keep asking for it
    	}
    	else // not really necessary to have else, but maybe clearer
    	{
    		cout << "acces denied\n";
    		cin >> password
    	}
    }
    We were all noobs once
    Last edited by smokeyangel; 08-10-2012 at 10:09 PM. Reason: fix indentation

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Quote Originally Posted by smokeyangel View Post
    This is a very confused line of code:

    Code:
     for ( int password; password == "pass";    )
    It reminds me a bad attempt of an enhanced for in JAVA

  15. #15
    Registered User
    Join Date
    Aug 2012
    Posts
    6
    thank you all, this forum is awesome!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  2. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Visual Studio Linker problem or my problem?
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-13-2004, 12:32 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 08:14 AM

Tags for this Thread


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