-4

Why does my code not run properly? As soon as it gets to the if else statement it takes one input from the user and then exits before I can enter anything else. I am not sure if it is due to the function not returning properly but I would really appreciate some help. Thanks.

#include <iostream>
#include <fstream>

using namespace std;

void studentenrollment (char answer);

int main()
{
    char answer;                                                                    //declaring variables for main
    cout << "Welcome to Luton Sixth Form" << endl;                                   //greeting the user
    cout << "Please State if you are enrolled or not at the sixth form: Y/N" << endl;//giving user options
    cin >> answer;//taking options from user
    studentenrollment(answer);                                                       //calling student enrollment function 

    return 0;
}

void studentenrollment (char answer)
{
    unsigned char name;
    int dob;
    if (answer=='Y'||answer=='y')
    {

        cout << "Welcome to higher education" << endl;
        cout << "Please state your name" << endl;
        cin >> name;
        ofstream myfile;
        myfile.open("StudentAccess.txt");
        myfile << name << endl;
        myfile.close();
        cout << "Your name is now saved, you have access to the gateway" << endl;
    }

    else if(answer=='N'||answer=='n')
    {
        cout << "Please state your name" << endl;
        cin >> name;
        cout << "Please enter your date of birth" << endl;
        cin >> dob;
        ofstream myfile;
        myfile.open("StudentEnrollment.txt");
        myfile << name << dob << endl;
        myfile.close();
        cout << "You will now go through enrollment" << endl;
    }

//    return 0;
}
Tay2510
  • 5,748
  • 7
  • 39
  • 58
Josh Marshall
  • 159
  • 3
  • 11
  • I do not currently have a debugger set up as I am just in the early stages of C++ – Josh Marshall Mar 24 '15 at 09:24
  • @JoshMarshall, what do you enter first? If you enter neither 'y' nor 'n' the program will exit. – vahancho Mar 24 '15 at 09:26
  • If you are on Linux, just install gdb plus clang or gcc, compile with '-g' for having debug info in the binary, and start debugging... Takes less than 5 minutes to set up. – Erik Alapää Mar 24 '15 at 09:27
  • 2
    You are trying to store a name in a `char` variable. You might want a character array instead. – Noel Mar 24 '15 at 09:28
  • 1
    @JoshMarshall having a debugger should come before writing code. I can't play this up enough - you need a debugger & to know how to debug. – Luchian Grigore Mar 24 '15 at 09:28
  • You tagged with "Visual Studio 2010". If you're actually using that, you have a debugger. – JBL Mar 24 '15 at 09:29
  • using Windows 8.1 OS.. I Enter 'Y' or 'N', if I enter 'N' the else if statement lets me enter the first input then closes – Josh Marshall Mar 24 '15 at 09:29
  • @JoshMarshall _" I do not currently have a debugger set up as I am just in the early stages of C++"_ You also have a debugger available if you're a beginner. That actually doesn't matter. It's your 1st tool at hand, nothing _"advanced"_! – πάντα ῥεῖ Mar 24 '15 at 09:34
  • 1
    @JoshMarshall also, if you are beginning c++, you don't need a full fledged debugger to debug code. In your case, simply checking the value stored in name after cin >> name would have been enough to catch your bug. This is most easily done by printing the variable "name" to the console using cout << name. Good luck with c++! – Darvish Kamalia Mar 24 '15 at 10:00
  • Thankyou guys, I am installing the windows developer kit now so I can set a debugger up as I can see that it is needed. I solved the problem though thanks again. – Josh Marshall Mar 24 '15 at 10:08

3 Answers3

1

unsigned char name; looks incorrect. Choose char name[MAX_LENGTH]; or std::string name;

What happens

cin >> name;  // Read just first character
cin >> dob;   // Try to read number, where rest of the name is left in the stream buffer

This certainly looks wrong unless the name is 1 letter wide.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • Thankyou - that solved the issue I had been having, is using string better practice than using char[MAX_LENGTH]? – Josh Marshall Mar 24 '15 at 09:47
  • 1
    @JoshMarshall Yes it certainly is for [multiple reasons](http://stackoverflow.com/questions/801209/char-vs-stdstring-in-c). – Mohit Jain Mar 24 '15 at 10:03
1

Problem can be this:

cin >> name;

you are entering name but storing it in name - which is just unsigned char. Use a larger array to store the name.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
0

If the variable name can contain more than 1 character you cannot declare it unsigned char, you can declare it std::string. remember the

#include<string>