Not getting expected output

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;

struct Date{
    int day,month,year;
};

struct Time{
    int hours,minutes;
};

struct Exam{
    Date d;
    Time t;

    string course_name;
    int RoomNum;


};

void getData(Exam&);

int main() {
    cout << "Enter your exam Information" << endl;
    Exam e[3];
    for (int i = 0; i < 1; i++) {
        getData(e[i]);

    }

    for (int j = 0; j < 1; j++) {
        cout<<e[j].course_name<<" : "<<e[j].d.month<<"-"<<e[j].d.day<<"-"<<e[j].d.year<<" "<<"at "<<e[j].t.hours<<":"<<e[j].t.minutes<<" "
            <<" in Room "<<e[j].RoomNum;

    }


}

void getData(Exam&) {
    Exam e;



        cout << "Enter the month of your exam" << endl;
        cin >> e.d.month;
        cout << "Enter the day of your exam" << endl;
        cin >> e.d.day;
        cout << "Enter the year of your exam" << endl;
        cin >> e.d.year;

        cout << "Enter the hours of your exam" << endl;
        cin >> e.t.hours;
        cout << "Enter the min of your exam" << endl;
        cin >> e.t.minutes;
    cin.ignore();
        cout << "Enter the course name of your exam" << endl;
        getline(cin,e.course_name);
    cout << "Enter the Room number of your exam" << endl;
        cin >> e.RoomNum;


}

0. You have dynamic user input without any examples... what is example of problematic input?
1. What's the Expected output?
2. What's the Actual output?

Other useful info -- only certain inputs fail, or does every input fail?
Hello NGC3370,

Without knowing what output you are getting and what output you expect. I do not know what to look for.

Andy
Btw, lines 41-42 look like syntax error. The implementation of the method should probably be
1
2
3
4
void getData(Exam& e) 
{ 
//... 
}

(You don't want to declare another Exam in addition to the one coming in as a reference)
Hello NGC3370,

The first thing I found is getting the user input.

The "getData" function definition looks good for a prototype, but will not work as a function definition. You forgot the variable name. The "Exam e;" you defined inside the function is lost when the function ends. It may be a personal thing with me, but I like the prototype to match the function definition. This way there is less chance of missing something like the variable name in the function definition.

So void getData(Exam&) should look like void getData(Exam& e).

I changed the final lines of main to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (int j = 0; j < 1; j++)
{
	cout << e[j].course_name << " : ";
	(e[j].d.month < 10 ? std::cout << "0" << e[j].d.month : std::cout << e[j].d.month) << "-";
	(e[j].d.day < 10 ? std::cout << "0" << e[j].d.day : std::cout << e[j].d.day) << "-";
	std::cout << e[j].d.year << " " << "at ";
	(e[j].t.hours < 10 ? std::cout << "0" << e[j].t.hours : std::cout << e[j].t.hours) << ":";
	(e[j].t.minutes < 10 ? cout << "0" << e[j].t.minutes : cout << e[j].t.minutes)
		<< " in Room " << e[j].RoomNum;
}

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();

return 0;


Hope that helps,

Andy
Topic archived. No new replies allowed.