Status
Not open for further replies.

shattered space

Distinguished
Dec 18, 2010
61
0
18,630
So I have been staring at this code for the past 2 hours...For some reason it won't let the user input something for everything question.
Code:
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
int main(){
    int memberchoice;
    int informationcorrectchoice;
    cout << "Startup Employee Informatin.\n";
    cout << "Are you a member?" << endl;
    cout << "1. Yes" << endl;
    cout << "2. No" << endl;
    cin >> memberchoice;
    if(memberchoice == 1){
            cout << "Please enter information.\n";
                do{
                    cout << "Username: \n";
                    char username[50];
                    cin >> username;
                    cout << "Processing: " << username << "\n";
                    cout << "Password: " << endl;
                    char password[50];
                    gets(password);
                    cout << "Processing: " << password << "\n";
                    cout << "Social Security Number: \n";
                    char ssnumber[11];
                    gets(ssnumber);
                    cout << "Processing: " << ssnumber << "\n";
                    cout << "Security Question: \n";
                    char securityquestion[3000];
                    gets(securityquestion);
                    cout << "Processing: " << securityquestion << "\n";
                    cout << "Security Answer: \n";
                    char securityanswer[3000];
                    gets(securityanswer);
                    cout << "Processing: " << securityanswer << "\n";
                    cout << username << "\n";
                    cout << password << "\n";
                    cout << ssnumber << "\n";
                    cout << securityquestion << "\n";
                    cout << securityanswer << "\n";
                    cout << "Is this information correct? \n";
                    cout << "1. Yes\n";
                    cout << "2. No\n";
                    cout << "Please enter 1 or 2 into the keyboard then press enter.\n";
                    cin >> informationcorrectchoice;
                } while (informationcorrectchoice != 1);
                    cout << "INFORMATION ACQUIRED...\n";
                    cout << "NOW PROCESSING...\n";
                    cout << "Validating...Validating...Validating.\n";
                    cout << "Login Successful\n";
                    cout << "Loading...Loading...Loading\n";
                    cout << "Main Menu Loaded...\n ";
                    cout << "\t\t\t Main Menu \t\t\t\n";
                    cout << "Projects \t Files \t Classified \t Schedule\n";
    } else{
            cout << "Please contact technical support.\n";
    }
    return 0;
}
Any help would be appreciated.
 
Solution
I would stop staring and start debugging, you might get further. ;)

Seriously though, without going into a huge debugging lesson, you have several ways to solve this problem. It's also a good chance to learn _necessary_ basic debugging skills since this is obviously a simple "learning to program" app.

1) Brute force using IDE - if you have a nice IDE & debugger, set a breakpoint somewhere early, where you know the code still works. Then just step through the code line by line, making sure the results are what you expected as you progress.

OR

2) Brute force using code - sprinkle those "cout" statements all over the place to 'tell' you the state of your variables and your location in the code. Then just follow along when you...

JustLurking

Distinguished
May 23, 2012
86
0
18,660
I would stop staring and start debugging, you might get further. ;)

Seriously though, without going into a huge debugging lesson, you have several ways to solve this problem. It's also a good chance to learn _necessary_ basic debugging skills since this is obviously a simple "learning to program" app.

1) Brute force using IDE - if you have a nice IDE & debugger, set a breakpoint somewhere early, where you know the code still works. Then just step through the code line by line, making sure the results are what you expected as you progress.

OR

2) Brute force using code - sprinkle those "cout" statements all over the place to 'tell' you the state of your variables and your location in the code. Then just follow along when you run it so you know the code is progressing as you expected.

OR

3) Break it down into the smallest possible program that works, then gradually put it back together piece by piece.

Some other tips - look for some of the the obvious problems first -- Variables initialized properly, semi-colons in the right places (especially during flow-control like do's and while's), code flowing the way you expect it to, case sensitivity, variable naming conflicts, yada yada yada.

Also make sure you're not testing variables without initializing them first.

Using these tips, you should have this debugged in just a few mins. :)

Good luck!
 
Solution

cavan21

Distinguished
Aug 2, 2011
25
0
18,540
Have you got your compillers and debugging options right.
I dont really work with c++ more of a c# python and java person and so if there was to be any errors check where it is. trust me you may have put one of the stupidest mistakes like grammer.
Always hated checking my grammer ;)
 

shattered space

Distinguished
Dec 18, 2010
61
0
18,630
I will go through every step you guys have given me...and yes I am learning, but it's more or less of I wanted to get the do{}while() loop down so that I will feel comfortable moving on in my book. I also have to get a minor thing with arrays down also. But thanks.
 

randomizer

Champion
Moderator
gets() is a bit of a dodgy function. It doesn't care that your username array is 50 elements in size, it will just keep on reading every character that gets entered and overwriting memory outside your buffer. It's dangerous, and you should use another function.

Now the actual problem is probably being cause by gets() leaving a newline character at the end of the input stream once it is finished reading. Newline and EOF characters are what it uses to delimit input, but it doesn't actually add them to the buffer. When the next call to gets() is made, it grabs the newline character still sitting in your input stream and immediately stops reading. Of course it still leaves that newline character there for the next call as well ;)
 


Use anything other then gets(); very unreliable C function. getline(cin, buffer) is very, very much preferred.

And take this as a lesson in debugging and stepping through code. If you do any serious SW work, you'll be doing more of that then actual coding. [Trust me on this].
 

hdeezie80

Honorable
Jul 18, 2012
205
0
10,710
It compiled fine for me but you need to work on your style, try using std::string as opposed to c strings it makes input and output a lot easier, also try making your variable names a bit shorter and camel case them instead of securityanswer type securityAnswer, and use spacing especially between control structures, it will make it a lot easier to spot errors next time.
 

alfredhyper

Honorable
Jan 19, 2013
1
0
10,510


try this it works out
 
Status
Not open for further replies.