C++ Code Help

These are the tasks:

Task 1 - Menu system. (7 points)
The first task is to create a menu system that will drive the program. The user will be able to create an arithmetic table for each of the four basic operators (+,-,*,/). Give the user a menu to choose which of these operators they wish to do.

Example:

Welcome to Math Study guide.
Which arithmetic table would you like to see?

1) Addition
2) Subtraction
3) Multiplication
4) Division
X) Exit the program
>>:

This menu should loop while the user enters invalid responses. The menu loop should stop when the user either enters a value to quit (-1 for example, or a character such as 'X') or a valid input.

Note: You can choose your method of input. If you want to process strings or characters go ahead!

Task 2 - Further input and If-Else If-Else or Switch to handle menu choice (6 points) Now you will get the boundaries for the table.

After getting the menu choice - regardless of which option is chosen you should now get the boundaries for the tables. You will get two numbers since you will be creating an X by Y (2 dimensional) table. Prompt the user for their choices. You should get the input in the format of "x y" (two number with a space in between).

Hint: input doesn't require a prompt to work syntactically, and numerical input reads numbers until it sees any whitespace

If either value is <= 0 tell the user that this is invalid and get the input again.

Example:

Enter the dimensions of the arithmetic table (x y):
Create either an If-Else If-Else chain or use a Switch Statement to handle the menu option.

Create this structure first before filling in the details (part 3).

Task 3 - Generate the appropriate arithmetic table (7 points)
For each math of the math operators generate the appropriate table from 1 to the value provided by the user performing the operator of choice.

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
100
101
102
103
104
105
106
107
108
109
110
111
  #include <iostream>
using namespace std;

int main()
{
    char input;
    int x=0;
    int y=0;
    //Display Welcome message
    cout << "Welcome to the Math Study Guide!\n";
    cout << "Which arithmetic table would you like to see?!\n"; // add newline on end
    
    do
    {
        cout << "1 Addition\n";
        cout << "2 Substraction\n";
        cout << "3 Multiplication\n";
        cout << "4 Division\n";
        cout << "X Exit the program\n";
        cout << ">>\n";
        cin >> input;
        
        if (input == 'X'){
            return 0;
        }
        
    }while(!(input == '1' || input == '2' || input == '3' || input == '4'));
    // check if the input is valid or not, if it isn't , repeat
    
    
    while (1){
        cout << "Choose x: \n";
        cin >> x;
        cout << "Choose y: \n";
        cin >> y;
        
        if (x <= 0 || y <= 0){
            cout << "You have entered an invalid dimension. Please enter a value greater than 0 for both dimensions.\n";
            cout << "Please enter the dimensions of the table (eg: 4 4): \n";
        }
        else{
            break; // break out of loop
        }
    }
} // ending int main ()
    switch (input)
{
    case '1':
        // display the table for addition
        println("\nYou have selected addition with a table size of " + x + " by " + y + ":");
        for(double i = 1; i <= x; i++)
        {
            for(double j = 1; j <= y; j++)
            {
                cout("%8.2f", (i + j));
            }
            printf();
        }
        break;
        
    case '2':
        // display the table for subtraction
        println("\nYou have selected subtraction with a table size of " + x + " by " + y + ":");
        for(double i = 1; i <= x; i++)
        {
            for(double j = 1; j <= y; j++)
            {
                printf("%8.2f", (i - j));
            }
            println();
        }
        break;
        
    case '3':
        // display the table for multiplication
        println("\nYou have selected multiplication with a table size of " + x + " by " + y + ":");
        for(double i = 1; i <= x; i++)
        {
            for(double j = 1; j <= y; j++)
            {
                printf("%8.2f", (i * j));
            }
            println();
        }
        break;
        
    case '4':
        // display the table for division
        println("\nYou have selected division with a table size of " + x + " by " + y + ":");
        for(double i = 1; i <= x; i++)
        {
            for(double j = 1; j <= y; j++)
            {
                printf("%8.2f", (i / j));
            }
            println();
        }
        break;
        
    case 'X':
        // display the exit message
        println("Thank you");
        break;
        
    default:
        // display the error message
        println("That is an invalid selection! Please choose 1-4 or X to exit.");

while(response != 'X');
 // end of main method
}
Last edited on
None of the code from line 46 onwards is in a function. That's bad.

Your main function finishes on line 45. So does your program. Your program ends when main does.
Last edited on
Now your assignment is clear. What is your question/problem?
I am getting a few errors with my code, the switch function, the println(), as well as my string
I can't see your screen from where I'm sitting. Maybe you should tell us the first error.

Have you fixed the problem yet of all the code from line 46 onwards not being inside a function? And the problem about how your program ends on line 45 at the end of main, and all the code after it never gets run?

the switch function

There's no such function in your code. You can't have a function named switch, and indeed you don't have any function with switch in.

println()

What's println() ? What programming language are you trying to write in?
Last edited on
fixed the line 46, dont know how to fix the rest :(
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
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
using namespace std;

int main()
{
    char input;
    int x=0;
    int y=0;
    //Display Welcome message
    cout << "Welcome to the Math Study Guide!\n";
    cout << "Which arithmetic table would you like to see?!\n"; // add newline on end
    
    do
    {
        cout << "1 Addition\n";
        cout << "2 Substraction\n";
        cout << "3 Multiplication\n";
        cout << "4 Division\n";
        cout << "X Exit the program\n";
        cout << ">>\n";
        cin >> input;
        
        if (input == 'X'){
            return 0;
        }
        
    }while(!(input == '1' || input == '2' || input == '3' || input == '4'));
    // check if the input is valid or not, if it isn't , repeat
    
    
    while (1){
        cout << "Choose x: \n";
        cin >> x;
        cout << "Choose y: \n";
        cin >> y;
        
        if (x <= 0 || y <= 0){
            cout << "You have entered an invalid dimension. Please enter a value greater than 0 for both dimensions.\n";
            cout << "Please enter the dimensions of the table (eg: 4 4): \n";
        }
        else{
            break; // break out of loop
        }
    }
    // ending int main ()
    switch (input)
{
    case '1':
        // display the table for addition
        printf("\nYou have selected addition with a table size of " + x + " by " + y + ":");
        for(double i = 1; i <= x; i++)
        {
            for(double j = 1; j <= y; j++)
            {
                cout<<"%8.2f", (i + j);
            }
            printf();
        }
        break;
        
    case '2':
        // display the table for subtraction
        printf("\nYou have selected subtraction with a table size of " + x + " by " + y + ":");
        for(double i = 1; i <= x; i++)
        {
            for(double j = 1; j <= y; j++)
            {
                printf("%8.2f", (i - j));
            }
            println();
        }
        break;
        
    case '3':
        // display the table for multiplication
        printf("\nYou have selected multiplication with a table size of " + x + " by " + y + ":");
        for(double i = 1; i <= x; i++)
        {
            for(double j = 1; j <= y; j++)
            {
                printf("%8.2f", (i * j));
            }
            println();
        }
        break;
        
    case '4':
        // display the table for division
        printf("\nYou have selected division with a table size of " + x + " by " + y + ":");
        for(double i = 1; i <= x; i++)
        {
            for(double j = 1; j <= y; j++)
            {
                printf("%8.2f", (i / j));
            }
            printf();
        }
        break;
        
    case 'X':
        // display the exit message
        printf("Thank you");
        break;
        
    default:
        // display the error message
        println("That is an invalid selection! Please choose 1-4 or X to exit.");

while(response != 'X');
 // end of main method
} // end of Assignment3 class 
Last edited on
I can't see your screen from where I'm sitting. Maybe you should tell us the first error. The first thing that your compiler complains about.

What is println? What is line 109 doing?
Last edited on
New code:
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
100
101
102
103
104
105
106
107
108
109
#include <iostream>
using namespace std;

int main()
{
    char input;
    int x=0;
    int y=0;
    //Display Welcome message
    cout << "Welcome to the Math Study Guide!\n";
    cout << "Which arithmetic table would you like to see?!\n"; // add newline on end
    
    do
    {
        cout << "1 Addition\n";
        cout << "2 Substraction\n";
        cout << "3 Multiplication\n";
        cout << "4 Division\n";
        cout << "X Exit the program\n";
        cout << ">>\n";
        cin >> input;
        
        if (input == 'X'){
            return 0;
        }
        
    }while(!(input == '1' || input == '2' || input == '3' || input == '4'));
    // check if the input is valid or not, if it isn't , repeat
    
    
    while (1){
        cout << "Choose x: \n";
        cin >> x;
        cout << "Choose y: \n";
        cin >> y;
        
        if (x <= 0 || y <= 0){
            cout << "You have entered an invalid dimension. Please enter a value greater than 0 for both dimensions.\n";
            cout << "Please enter the dimensions of the table (eg: 4 4): \n";
        }
        else{
            break; // break out of loop
        }
    }
switch (input)
{
    case '1':
        // display the table for addition
        printf("\nYou have selected addition with a table size of " + x + " by " + y + ":");
        for(double i = 1; i <= x; i++)
        {
            for(double j = 1; j <= y; j++)
            {
                printf("%8.2f", (i + j));
            }
            
        }
        break;
        
    case '2':
        // display the table for subtraction
        printf("\nYou have selected subtraction with a table size of " + x + " by " + y + ":");
        for(double i = 1; i <= x; i++)
        {
            for(double j = 1; j <= y; j++)
            {
                printf("%8.2f", (i - j));
            }
            
        }
        break;
        
    case '3':
        // display the table for multiplication
        printf("\nYou have selected multiplication with a table size of " + x + " by " + y + ":");
        for(double i = 1; i <= x; i++)
        {
            for(double j = 1; j <= y; j++)
            {
                printf("%8.2f", (i * j));
            }
            
        }
        break;
        
    case '4':
        // display the table for division
        printf("\nYou have selected division with a table size of " + x + " by " + y + ":");
        for(double i = 1; i <= x; i++)
        {
            for(double j = 1; j <= y; j++)
            {
                printf("%8.2f", (i / j));
            }
            
        }
        break;
        
    case 'X':
        // display the exit message
        printf("Thank you");
        break;
        
    default:
        // display the error message
        printf("That is an invalid selection! Please choose 1-4 or X to exit.");

while(input != 'X');
}


Errors: With string use on lines 50,63,76,89
Lines 50,63,76,89: printf() expects a simple C-string for the format specifier. C-strings don't support concatenation with the + operator. Why not use cout?
 
cout << "\nYou have selected addition with a table size of " << x << " by " << y << ":";

Topic archived. No new replies allowed.