Printing error

ok so this is my code for some Movie rental thing there seems to be no problem during compile time but in runtime when i enter some character in cin >> choice; (choice variable) it jumps couple of steps forward and instead of printing (cout << "Please provide the following info:";) it creates a mess, run it ur selves and u will see:
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using namespace std;

int main()
{
    char cname[20],mname[20],choice;
    int days;
    cout << "Please give customer name: ";
    cin.getline(cname,19);
    cout << "Please provide movie description" << endl;
    cout << "Enter 'R' for Romance Movie" << endl;
    cout << "Enter 'P' for Popular Movie" << endl;
    cout << "Enter 'N' for New Release" << endl;
    cout << "Enter 'K' for Kids" << endl;
    
    cin >> choice;
    cout << "Please provide the following info:" << endl;
    cout << "Movie Name? ";
    
    cin.getline(mname,19);
    cout << "Number of day's? ";
    cin >> days;
    //-----------output----------
    cout << "Customer Name: " << cname << endl;
    cout << "Movie Type: ";
    switch (choice)
    {
           case 'R':
                cout << "Romance Movie" << endl;
                break;
           case 'r':
                cout << "Romance Movie" << endl;
                break;
           case 'P':
                cout << "Popular Movie" <<endl;
                break;
           case 'p':
                cout << "Popular Movie" << endl;
                break;
           case 'N':
                cout << "New Release" << endl;
                break;
           case 'n':
                cout << "New Release" << endl;
                break;
           case 'K':
                cout << "Kids Movie" << endl;
                break;
           case 'k':
                cout << "Kids Movie" << endl;
                break;
                }
    cout << "Movie Name: " << mname << endl;
    cout << "Number of Day's: " << days << endl;
        int totalamount;
    switch (choice)
    {
           case 'R':
                totalamount = days*40;
                break;
           case 'r':
                totalamount = days*40;
                break;
           case 'P':
                totalamount = days*80;
                break;
           case 'p':
                totalamount = days*80;
                break;
           case 'N':
                totalamount = days*120;
                break;
           case 'n':
                totalamount = days*120;
                break;
           case 'K':
                totalamount = days*20;
                break;
           case 'k':
                totalamount = days*20;
                break;
                }
    cout << "Your Rental Amount is: " << totalamount << endl;
    system ("PAUSE");
    return 0;
}

can some one tell me wats wrong with it plz!
closed account (3hM2Nwbp)
Hello, I believe that you need to ignore some characters from the input stream. Try this:

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
using namespace std;

int main()
{
    char cname[20],mname[20],choice;
    int days;
    cout << "Please give customer name: ";
    cin.getline(cname,19);
    cout << "Please provide movie description" << endl;
    cout << "Enter 'R' for Romance Movie" << endl;
    cout << "Enter 'P' for Popular Movie" << endl;
    cout << "Enter 'N' for New Release" << endl;
    cout << "Enter 'K' for Kids" << endl;

    cin >> choice;
    /*NEW*/

    //Ignore characters until '\n' is reached
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    /*NEW*/

    cout << "Please provide the following info:" << endl;
    cout << "Movie Name? ";

    cin.getline(mname,19);
    cout << "Number of day's? ";
    cin >> days;
    /*NEW*/
    //Ignore characters until '\n' is reached
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    /*NEW*/
    //-----------output----------
    cout << "Customer Name: " << cname << endl;
    cout << "Movie Type: ";
    switch (choice)
    {
           case 'R':
                cout << "Romance Movie" << endl;
                break;
           case 'r':
                cout << "Romance Movie" << endl;
                break;
           case 'P':
                cout << "Popular Movie" <<endl;
                break;
           case 'p':
                cout << "Popular Movie" << endl;
                break;
           case 'N':
                cout << "New Release" << endl;
                break;
           case 'n':
                cout << "New Release" << endl;
                break;
           case 'K':
                cout << "Kids Movie" << endl;
                break;
           case 'k':
                cout << "Kids Movie" << endl;
                break;
                }
    cout << "Movie Name: " << mname << endl;
    cout << "Number of Day's: " << days << endl;
        int totalamount;
    switch (choice)
    {
           case 'R':
                totalamount = days*40;
                break;
           case 'r':
                totalamount = days*40;
                break;
           case 'P':
                totalamount = days*80;
                break;
           case 'p':
                totalamount = days*80;
                break;
           case 'N':
                totalamount = days*120;
                break;
           case 'n':
                totalamount = days*120;
                break;
           case 'K':
                totalamount = days*20;
                break;
           case 'k':
                totalamount = days*20;
                break;
                }
    cout << "Your Rental Amount is: " << totalamount << endl;
    system ("PAUSE");
    return 0;
}


Here's some food for thought :)

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef _MOVIE_PROMPTER_HPP_
#define _MOVIE_PROMPTER_HPP_

/*
 * This is just what I like to do, but instead of having the 'using namespace' clause
 * I prefer to individually document what's used.  Probably a lot of people won't agree and
 * will think it's just extra unnecessary code.
 */

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::getchar;
using std::numeric_limits;
using std::streamsize;

/**
 * The concept modularized in a class
 */

class MoviePrompter
{
public:
	static void PromptForMovie(void)
	{
		char customerName[20], movieName[20];
		unsigned int numberOfDays;
		unsigned char choice;
		cout << "Please give customer name: ";
		cin.getline(customerName, 19);
	    cout << "Please provide movie description" << endl;
		cout << "Enter 'R' for Romance Movie" << endl;
		cout << "Enter 'P' for Popular Movie" << endl;
		cout << "Enter 'N' for New Release" << endl;
		cout << "Enter 'K' for Kids" << endl;
		choice = toupper(getchar()); // notice the toupper method - it will set the input to uppercase if possible
		//Validate the user's input
		while(choice != 'R' && choice != 'P' && choice != 'N' && choice != 'K')
		{
			cout << "Invalid Selection" << endl;
		    cout << "Please provide movie description" << endl;
			cout << "Enter 'R' for Romance Movie" << endl;
			cout << "Enter 'P' for Popular Movie" << endl;
			cout << "Enter 'N' for New Release" << endl;
			cout << "Enter 'K' for Kids" << endl;
			choice = toupper(getchar());
			cout << endl;
		}
		//Ignore the extraneous data in the stream
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		cout << "Please provide the following info:" << endl;
		cout << "Movie Name? ";
		cin.getline(movieName, 19);
		cout << endl;
		cout << "Number of days? ";
		cin >> numberOfDays;
		//Here, we check for input failure.  This occurs, for example, if you try to put 'adasfaewdfsfa' into an integer variable
		while(!cin)
		{
			cin.clear(); //reset the fail state
			cin.ignore(numeric_limits<streamsize>::max(), '\n'); //Ignore the extraneous data
			cout << "Invalid entry" << endl;
			cout << "Numer of days? ";
			cin >> numberOfDays;
		}
		int totalAmount;
		switch (choice)
		{
			case 'R':
				totalAmount = numberOfDays*40;
				break;
			case 'P':
				totalAmount = numberOfDays*80;
				break;
			case 'N':
				totalAmount = numberOfDays*120;
				break;
			case 'K':
				totalAmount = numberOfDays*20;
				break;
		}
		cout << "Your Rental Amount is: " << totalAmount << endl;
	}
};

#endif // _MOVIE_PROMPTER_HPP_

/*
 * Now, to use our class, see below
 */

int main()
{
	//PromptForMovie is a static member function of the MoviePrompter class.
	MoviePrompter::PromptForMovie();
	return 0;
}
Last edited on
thx a ton Luc!
Topic archived. No new replies allowed.