No, this is not an assignement. Recently I decided to pick up writing code again and the project I started writing I came to a rode block with something that I never learned in class (I don't think).

How would I be able to write this better (should I stick with the case statements or should I switch to if statements or something completely different?) so that if someone puts in the wrong password it will return them to the beginning to line 13? This is the code I have written so far:

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#include <cstdio>
#include <conio.h>

using namespace std;

int main()
{
    char cPass;
    cout << "Hello user. Please input your password:" << endl;
    cin >> cPass;

    switch(cPass)
    {
        case 'correct':
        {
            cout << "Welcome Walker. What course are we running today?" << endl;
        }
       /*This is where I run into my question.*/
       case 
        {
            system ("cls");
            cout << "Invalid password. Please try again." << endl;
            return int main;
        }
    }


    return 0;
}

It's been a couple of years since I wrote code so my mind draws a blank. I've looked on some other sites and I can't seem to find an answer to my question. Any help would be spectacular!
-Walker

if someone puts in the wrong password it will return them to the beginning to line 13?

use a loop

Hey,

You're not breaking your cases, after each case you need to have a break otherwise it may just jump to the next case etc. Also, you need to have/suggested that you always have a default case. Here's an example:

int main()
{
    int number;

    switch(number)
    {
        case 0:
         // case 0
        break;

        case 1:
        // case 1
        break;

        default:
          cout << "This is the default case";
        break;

    }

}

As another note, in the example you provided, are switch statements really useful? You only have two outcomes, which, can be handled with an IF statement. Usually, (personally) I use switch statements when handling menues, or if I have multiple options.

P.S. The output of "cPass" will only be "sucsess" if the user enters that word. So if you want the password to be "bananas" the outcome will always be false. You need to store the password (Either in a text file, or, for not, in a constant variable) and then check if that word, is the entered word.

Hope this helps :)

commented: Yes it has helped! The if statement went over much better then the case statement. Thank you! +0

FYI, char cPass; defines one character. There is no way you can cram "correct" into one character.

As others have noted, you're attempting to store a string in a single character, so that needs addressing first.

Once you have your input string, you'll want to compare it with the correct password, which I assume will also be a string (given the simplicity of your program). Rather than using a case statement, then, you'll want to use string comparison to determine whether the input string matches the correct password, and then select an appropriate response.

In order to enable another attempt if the input doesn't match the password, consider the use of a loop.

Most of your problems here appear to be associated with a weakness in your understanding of C-style strings, i.e. how to store them and how to compare them. It's worth spending a little time becoming familiar with string handling. At some point you may want to switch to C++ strings, if your environment supports them.

Where are you checking that password is correct or not ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.