Very Very New to C++. Help please!

Hey. I am VERY new to C++. Here is what I need to program in C++. I am using Dev CPP tool.

Create a C++ application that will ask the user for the number of watermelons. The application will output how many slices are available. There are 8 slices per watermelon. If the user inputs 2 watermelons the output should be 16 slices.

Here is what I have gotten so far.....

#include <iostream>
using namespace std;

int main()
{
int num_of_watermelons, slices_of_watermelon;

cout << "Press return after entering a number. \n";
cout << "Enter the number of watermelons:\n";

cin >> number_of_watermelons;

cout << "Enter the number of watermelons:\n";

cin >> number_of_watermelons;
slices_of_watermelon = number_of_watermelons * 8;
cout << "If you have ";
cout << "number_of_watermelons;
cout << "You have ";
cout << "number_of_slices";

return 0;
}


Can someone please tell me what I am doing wrong? Again, I am very new to C++ but I am trying. Please help!
Last edited on
You need to properly format your code before posting
http://www.cplusplus.com/articles/z13hAqkS/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
 {
    int num_of_watermelons, slices_of_watermelon;

    cout << "Press return after entering a number. \n";
    cout << "Enter the number of watermelons: ";
    cin >> number_of_watermelons;  //<<----------------------dosn't match your declareation num_of_watermelons

    slices_of_watermelon = number_of_watermelons * 8; //<<---dosn't match your declaration num_of_watermelons

    cout << "If you have ";
    cout << "number_of_watermelons; //<<---------------------you have an extra " here,  variable name is wrong should be num_of_watermelons
    cout << " You have ";
    cout << "number_of_slices";  //<<------------------------don't wan't " here, wrong name should be slices_of_watermelon

    return 0;
 }
Ok. What do I need to do to correct this so it works correctly? I am confused.
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
#include <iostream>
using namespace std;

int main()
{
    int num_of_watermelons ; /*, slices_of_watermelon; */

    cout << "Press return after entering a number. \n";
    cout << "Enter the number of watermelons:\n";

    //cin >> number_of_watermelons;
    cin >> num_of_watermelons ;

    //cout << "Enter the number of watermelons:\n";
    //cin >> number_of_watermelons;

    //slices_of_watermelon = number_of_watermelons * 8;
    const int slices_of_watermelon = num_of_watermelons * 8;

    /*
    cout << "If you have ";
    cout << "number_of_watermelons;
         cout << "You have ";
         cout << "number_of_slices";
    */

    cout << "if you have " << num_of_watermelons << " watermelons, you get "
         << slices_of_watermelon << " slices.\n" ;

    return 0;
}
The program is now loading, but when I run the program and it asks me to enter the number of watermelons and I enter the number and press enter, it instantly exits the program. How I do I correct this?
Make the program wait for the user to enter something.

Add this just before the return.

1
2
3
char c ;
cout << "enter any character to quit: " ;
cin >> c ;
That got it. Thanks SO much JLBorges! You really helped me out a LOT!!!!!!!
Topic archived. No new replies allowed.