A few coding errors

Mind showing me what I did wrong?
http://prntscr.com/bjcjv6
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
 //Ashton Dreiling
//Money exercise
#include <iostream>
#include <stdlib.h>

using namespace std;

//Module prototypes
void calculateTheValueOfPensNicksDimesQuarts (double pennies, double nickles, double dimes, double quaters, double &valueOfPennies, double &valueOfNickles, double &valueOfDimes, double &valueOfQuarters);

//Global constants that will be used to calculate the total amount the user has after they input values
const double costOfPenny = .01;
const double costOfNickle = .05;
const double costOfDime = .10;
const double costOfQuarter = .25;
const double aDollar = 1.00;

int main()
{
    //some variables to hold money values
    double pennies;
    double nickles;
    double dimes;
    double quarters;
    double valueOfPennies;
    double valueOfNickles;
    double valueOfDimes;
    double valueOfQuarters;
    double total;

    cout << "Hello, the point of this program is to make no more than one dollar. Let's see if you can do it." << endl;
    cout << "Please enter the number of pennies you have." << endl;
    cin >> pennies;
    cout << "You said you had " << pennies << endl;
    cout << "Please enter the number of nickles you have." << endl;
    cin >> nickles;
    cout << "You said you had " << nickles << endl;
    cout << "Please enter the number of dimes you have." << endl;
    cin >> dimes;
    cout << "You said you had " << dimes << endl;
    cout << "Please enter the number of quarters you have." << endl;
    cin >> quarters;
    cout << "You said you had " << quarters << endl;
    cout << "We will now calculate if you made a dollar or went over a dollar." << endl;

    //passvariables to calculateTheValueOfPensNicksDimesQuarts
    calculateTheValueOfPensNicksDimesQuarts (pennies, nickles, dimes, quarters, valueOfPennies, valueOfNickles, valueOfDimes, valueOfQuarters);
    //final amount to compare in an if-then statement to see if they went over a dollar of if they met exactly one dollar
    total = valueOfPennies + valueOfNickles + valueOfDimes + valueOfQuarters;
    cout << "Your total is " << total << endl;
    if (total == aDollar)
        cout << "Congratulations! You did it!" << endl;
    else
    {
        if (total < aDollar)
            cout << "Sorry, you had less than a dollar" << endl;
        else
        {
            if (total > aDollar)
                cout << "Sorry, you went over a dollar." endl;
            else }





    return 0;
        system("Pause");
}//end main

void calculateTheValueOfPensNicksDimesQuarts(double pennies, double nickles, double dimes, double quarters, double &valueOfPennies, double &valueOfNickles, double &valueOfDimes, double &valueOfQuarters)
{
    valueOfPennies = pennies * costOfPenny;
    valueOfNickles = nickles * costOfNickle;
    valueOfDimes = dimes * costOfDime;
    valueOfQuarters = quarters * costOfQuarter;

}//end of calculateTheValueOfPensNicksDimesQuarts module
closed account (37oyvCM9)
line 61: replace else } with }
line 60: replace cout << "Sorry, you went over a dollar." endl;
/// with cout << "Sorry, you went over a dollar." << endl;

try cutting and pasting that long arse function to prototype again lol.


oh yeah and try moving system pause above return 0; as it wont get read...umm hope that helps a bit... ill keep looking
Last edited on
Just need 73 and 78 : )

Also, can you confirm I did my program correctly?

I made no logic errors for example?
closed account (37oyvCM9)
no prob
closed account (37oyvCM9)
found the last error...they are combined just add a closing brace on line 70
I'm a bit confused. Why do I need to? Wouldn't they come after the module (void).

Can you explain?
I'm confused because in this code for eg (skip to bottom for explanation)

//Ashton Dreiling
//BMI Exercise
#include <iostream>
#include <stdlib.h>

using namespace std;
//module prototype
void calculatesBmi(double &BMI,double &weight, double &height);

//Global constant
const double number = 702;

int main()
{
//some variables
double weight;
double height;
double BMI;

cout << "Hello, today we are going to be calculating your BMI" << endl;
cout << "Please type in your weight in pounds" << endl;
cin >> weight;
cout << "You typed in " << weight << endl;
cout << "Please type in your height in inches." << endl;
cin >> height;
cout << "You typed in " << height << endl;
cout << "Please give us a second to calculate your BMI." << endl;

calculatesBmi(BMI,weight,height);
//pass variables to calculatesBmi module
cout << "Your BMI is " << BMI << endl;

system("Pause");


return 0;
}//end main

void calculatesBmi(double &BMI,double &weight, double &height)
{ //calculating BMI

BMI = weight / (height * height) * number;
}//end calculatesBmi

I didn't have to add a closing bracket above void

Why do I have to do it here?
closed account (37oyvCM9)
Hi found one more little problem...If you run your code like it is...when you get a dollar it closes without pause... you just need to change the last bit of your code to this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (total == aDollar)
		cout << "Congratulations! You did it!" << endl;
	else
	{
		if (total < aDollar)
			cout << "Sorry, you had less than a dollar" << endl;
		else
		{
			if (total > aDollar)
				cout << "Sorry, you went over a dollar." << endl;
		}




	}
		system("pause");
		return 0;

	//end main
}


Now all runs great...gives correct answers and logic works correctly....One little bit of advice, when you have a function like that with loads of parameters just bumb half down on the next line(makes it much easier to read...also try space your code out a lil bit....Other than that its great :)).
Last edited on
closed account (37oyvCM9)
No it wasn't your function declaration that was the problem, it was a curly brace problem with your nested if statements. Once they where put in the correct place the program runs awesome. so what you had before was.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if (total == aDollar)
        cout << "Congratulations! You did it!" << endl;
    else
    {
        if (total < aDollar)
            cout << "Sorry, you had less than a dollar" << endl;
        else
        {
            if (total > aDollar)
                cout << "Sorry, you went over a dollar." endl;
            else }





    return 0;
        system("Pause");
}//end main 

changed to this: compiler knows what to do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    if (total == aDollar)
   {
          cout << "Congratulations! You did it!" << endl;
    }
   else
   {
           if (total < aDollar)
           {
	           cout << "Sorry, you had less than a dollar" << endl;
           }
           else
	   {
	           if (total > aDollar)
		   {
		           cout << "Sorry, you went over a dollar." << endl;
		    }

             }

	}
	return 0;
	system("Pause");
}//end main 


always gotta watch curly braces...I know you can leave them off for one line if statements but generally just put them on anyway so your eyes can see where they finish

hope this helped sorry about the confusion.

Sorry not all lined up nicley no bloody tab functionality lol
Last edited on
For some reason, I replied, but my post isn't showing up. Just in case it didn't, I'll write again.

Would you mind showing me how to put parameters on another line correctly? I've never seen or done so, thus I was hesitant.

I noticed you had some extra brackets compared to the edit I made. Is this still correct?

if (total == aDollar)
cout << "Congratulations! You did it!" << endl;
else
{
if (total < aDollar)
cout << "Sorry, you had less than a dollar" << endl;
else
{
if (total > aDollar)
cout << "Sorry, you went over a dollar." << endl;
}





return 0;
system("Pause");
}//end main

Also, would you tell me why I had to add a bracket to line 70 for my program to work?
closed account (37oyvCM9)
The bracket on line 70 was the left over bracket from you if statements(you missed one).

With curly braces its probably best practice to always use both of them.

so instead of:
if(true)
then do something;

use this instead:
if(true)
{
then do something;
}

it makes it clearer for you(no missing braces that cause you weird errors) and clearer on ppl looking at your code.

with bumping parameters down to the next line. All you need to know is in most cases the compiler ignores all white space so.

 
void calculateTheValueOfPensNicksDimesQuarts(double pennies, double nickles, double dimes, double quarters, double &valueOfPennies, double &valueOfNickles, double &valueOfDimes, double &valueOfQuarters)

1
2
3
4
void calculateTheValueOfPensNicksDimesQuarts(double pennies, double nickles, double dimes,  double quarters, 
                                             double &valueOfPennies, double &valueOfNickles, doubl &valueOfDimes, 
                                             double &valueOfQuarters)


Again it just makes it easier for you to check for errors like missing curly braces, commas, semi colons.

Hope I helped :)


//Ashton Dreiling
//Money exercise
#include <iostream>
#include <stdlib.h>

using namespace std;

//Module prototypes
void calculateTheValueOfPensNicksDimesQuarts (int pennies, int nickles,
int dimes, int quaters, int &valueOfPennies,
int &valueOfNickles, int &valueOfDimes, int &valueOfQuarters);

//Global constants that will be used to calculate the total amount the user has after they input values
const int costOfPenny = .01;
const int costOfNickle = .05;
const int costOfDime = .10;
const int costOfQuarter = .25;
const int aDollar = 1.00;

int main()
{
//some variables to hold money values
int pennies;
int nickles;
int dimes;
int quarters;
int valueOfPennies;
int valueOfNickles;
int valueOfDimes;
int valueOfQuarters;
int total;

cout << "Hello, the point of this program is to make no more than one dollar. Let's see if you can do it." << endl;
cout << "Please enter the number of pennies you have." << endl;
cin >> pennies;
cout << "You said you had " << pennies << endl;
cout << "Please enter the number of nickles you have." << endl;
cin >> nickles;
cout << "You said you had " << nickles << endl;
cout << "Please enter the number of dimes you have." << endl;
cin >> dimes;
cout << "You said you had " << dimes << endl;
cout << "Please enter the number of quarters you have." << endl;
cin >> quarters;
cout << "You said you had " << quarters << endl;
cout << "We will now calculate if you made a dollar or went over a dollar." << endl;

//passvariables to calculateTheValueOfPensNicksDimesQuarts
calculateTheValueOfPensNicksDimesQuarts (pennies, nickles, dimes,
quarters, valueOfPennies, valueOfNickles,
valueOfDimes, valueOfQuarters);
//final amount to compare in an if-then statement to see if they went over a dollar of if they met exactly one dollar
total = valueOfPennies + valueOfNickles + valueOfDimes + valueOfQuarters;
cout << "Your total is " << total << endl;
if (total == aDollar)
cout << "Congratulations! You did it!" << endl;
else
{
if (total < aDollar)
cout << "Sorry, you had less than a dollar" << endl;
else
{
if (total > aDollar)
cout << "Sorry, you went over a dollar." << endl;
}





return 0;
system("Pause");
}//end main

void calculateTheValueOfPensNicksDimesQuarts(int pennies, int nickles, int dimes, int quarters,
int &valueOfPennies, int &valueOfNickles, int &valueOfDimes,
int &valueOfQuarters)
{
valueOfPennies = pennies * costOfPenny;
valueOfNickles = nickles * costOfNickle;
valueOfDimes = dimes * costOfDime;
valueOfQuarters = quarters * costOfQuarter;

}//end of calculateTheValueOfPensNicksDimesQuarts module


Ah I must have messed something up again : (

http://prntscr.com/bjejj7
These are the error I'm getting now

You're helping a lot! I changed the data type and put the parameters on different lines. That's all I did.
Last edited on
I looked at what I did wrong and fixed it! Thanks so much for your help!
closed account (37oyvCM9)
Your still missing your closing brace lol put the last closing brace on just above your return 0; at the end of int main. there should be three curly braces here, you have got your if statements all out of wack because you are not using both curly braces! :)

delete your code from the bracket after your return 0; in main, to the beginning of your if statements and put this in(AND DONT CHANGE IT LOL)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
        if (total == aDollar)
	{
		cout << "Congratulations! You did it!" << endl;
	}
	else
	{
		if (total < aDollar)
		{
			cout << "Sorry, you had less than a dollar" << endl;
		}
		else
		{
			if (total > aDollar)
			{
				cout << "Sorry, you went over a dollar." << endl;
			}
		}

	}
		return 0;
		system("Pause");
}//end main 


Last edited on
I got it! I marked this as solved, but I just ran it, and I'm getting a logic error.

I put in I have 4 quarters, but it's saying my total is 0. What am I doing wrong?

//Ashton Dreiling
//Money exercise
#include <iostream>
#include <stdlib.h>

using namespace std;

//Module prototypes
void calculateTheValueOfPensNicksDimesQuarts (int pennies, int nickles,
int dimes, int quaters, int &valueOfPennies,
int &valueOfNickles, int &valueOfDimes, int &valueOfQuarters);

//Global constants that will be used to calculate the total amount the user has after they input values
const int costOfPenny = .01;
const int costOfNickle = .05;
const int costOfDime = .10;
const int costOfQuarter = .25;
const int aDollar = 1;

int main()
{
//some variables to hold money values
int pennies;
int nickles;
int dimes;
int quarters;
int valueOfPennies;
int valueOfNickles;
int valueOfDimes;
int valueOfQuarters;
int total;

cout << "Hello, the point of this program is to make no more than one dollar. Let's see if you can do it." << endl;
cout << "Please enter the number of pennies you have." << endl;
cin >> pennies;
cout << "You said you had " << pennies << endl;
cout << "Please enter the number of nickles you have." << endl;
cin >> nickles;
cout << "You said you had " << nickles << endl;
cout << "Please enter the number of dimes you have." << endl;
cin >> dimes;
cout << "You said you had " << dimes << endl;
cout << "Please enter the number of quarters you have." << endl;
cin >> quarters;
cout << "You said you had " << quarters << endl;
cout << "We will now calculate if you made a dollar, had less than a dollar, or went over a dollar." << endl;

//passvariables to calculateTheValueOfPensNicksDimesQuarts
calculateTheValueOfPensNicksDimesQuarts (pennies, nickles, dimes,
quarters, valueOfPennies, valueOfNickles,
valueOfDimes, valueOfQuarters);
//final amount to compare in an if-then statement to see if they went over a dollar of if they met exactly one dollar
total = (valueOfPennies + valueOfNickles + valueOfDimes + valueOfQuarters);
cout << "Your total is " << total << endl;
if (total == aDollar)
cout << "Congratulations! You did it!" << endl;
else
{
if (total < aDollar)
cout << "Sorry, you had less than a dollar" << endl;
else
{
if (total > aDollar)
cout << "Sorry, you went over a dollar." << endl;
}
}




return 0;
system("Pause");
}//end main

void calculateTheValueOfPensNicksDimesQuarts(int pennies, int nickles, int dimes, int quarters,
int &valueOfPennies, int &valueOfNickles, int &valueOfDimes,
int &valueOfQuarters)
{
valueOfPennies = (pennies * costOfPenny);
valueOfNickles = (nickles * costOfNickle);
valueOfDimes = (dimes * costOfDime);
valueOfQuarters = (quarters * costOfQuarter);

}//end of calculateTheValueOfPensNicksDimesQuarts module
closed account (37oyvCM9)
lol hold on
You should turn your private messaging on. <3

Haha.
closed account (37oyvCM9)
change your integers back to doubles(#youwhererighttostartwith)....that will fix it for ya....your working with decimal point values so integers will just truncate anything after a whole number.
Topic archived. No new replies allowed.