I cant find the error

After I wrote this program I got a a lot of error messages but I cant find where I went wrong.

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
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main()
{
    string Description;
    int coupType;
    int coupVal;
    int price;
    int coupon;
    int saved;
    int due;
 
    cout << "Enter Description of Item: "; 
    cin >> Description;
    cout << endl;
    cout << "Enter Regular Price: "; 
    cin >> price;
    cout << endl;  
    cout << "Do you have a coupon (Y/N): ";  
    cin >> coupon;
    
    if (coupon == 'Y'){
        cout << "Enter coupon type (C/P): "; cin >> coupType;
            if (coupType == 'C') {
                cout << "Enter coupon value (cash off): ";
                cin >> coupVal;
                cout << endl;
                saved = coupVal;
                due = price - saved;
                cout << "ITEM DESCRIPTION: " << Description << endl << "REGULAR PRICE   = " << price << endl << "COUPON TYPE     = " << coupType << endl << "COUPON VALUE    = " << coupVal << endl << "AMOUNT SAVED    = " << saved << endl << "PAYMENT DUE     = " << due;
                else
                    cout << 'Enter coupon value (percentage off 1-100): ";
                    cin >> coupVal;
                    cout << endl;
                    saved = coupVal * price;
                    due = price - saved;
                    cout << "ITEM DESCRIPTION: " << Description << endl << "REGULAR PRICE   = " << price << endl << "COUPON TYPE     = " << coupType << endl << "COUPON VALUE    = " << coupVal << endl << "AMOUNT SAVED    = " << saved << endl << "PAYMENT DUE     = " << due;
                
            }
         else
             cout << "ITEM DESCRIPTION: " << Description << endl << "REGULAR PRICE   = " << price << endl << "PAYMENT DUE     = " << due;
    }
        
                



   return 0;

}//main 
Last edited on
This site's syntax highlighting hints at one mistake.

cout << 'Enter coupon value (percentage off 1-100): ";
You begin this with a single quote, but end with a double quote.

Strings literals always have double quotes " ". Individual character literals have single quotes ' '.


Multi-line bodies of if-statements MUST be surrounded by { } braces. Lines 37 to 43 suggests you want { } around them.

1
2
        cout << "Enter coupon type (C/P): "; cin >> coupType;
            if (coupType == 'C') {

Your indentation here is very misleading. These statements should all be on the same indentation level of code. Also, avoid putting more than one semi-colon on one line.

Right now you have it like this:
1
2
3
4
5
6
7
if (a)
{
    statement;
    statement;
    else
        statement;
}

The else has no if paired with it.
You need to do this:
1
2
3
4
5
6
7
8
9
if (a)
{
    statement;
    statement;
}
else
{
    statement;
}
Last edited on
I fixed so it compiles now, but it now skips over parts of the code this is what it looks like when I run it.

codio@sofia-trust:~/workspace$ ./a.out
Enter Description of Item: Toy

Enter Regular Price: 30

Do you have a coupon (Y/N): Y
ITEM DESCRIPTION: Toy
REGULAR PRICE = 30
PAYMENT DUE = 0codio@sofia-trust:~/workspace$

Hello NKGold,

Your first problem are line 25 and 26. On 25 you prompt the user to enter a character "Y" or "N". On line 26 "cin" is expecting you to enter a number. By entering a letter it causes "cin" to fail and is unusable for the rest of the program.

Although a "char" is a type of int it is not as interchangeable as you might think.

Your variables "coupon" and "coupType" should be defined as a "char" not an "int".

Another point: Do not expect the user to follow directions and enter only capital letters. Either make sure the case is correct with "std::tolower()" or "std::toupper()" from the "cctype" header file or use the if statement as:
if (coupon =='Y' || coupon == 'y') This way either method will deal with whatever the user enters. The same applies for the coupon code.

After I fixed all the problems the program works.

Hope that helps,

Andy
Thanks, that worked
Topic archived. No new replies allowed.