We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,085 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Case Statements

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

6
Contributors
5
Replies
7 Hours
Discussion Span
8 Months Ago
Last Updated
7
Views
andreasen12N
Newbie Poster
1 post since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

use a loop

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

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 :)

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

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

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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.

Bob
using namespace Bob
Team Colleague
221 posts since Feb 2003
Reputation Points: 30
Solved Threads: 11
Skill Endorsements: 0

Where are you checking that password is correct or not ?

np complete
Posting Whiz
385 posts since Sep 2010
Reputation Points: 18
Solved Threads: 36
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0833 seconds using 2.84MB