C++ Total not coming up correct. Must be right under the tip of my nose.

Ok so I'm taking my first programming class (intro to prig and logic) and have pretty limited computer knowledge. Im loving the class right now and have been pretty good at picking up on how this whole thing works. However, I have labs for units 5-10 due this week and for some reason there is one problem on unit 5 i just can't seem to figure out. I cruised through the later materials, but for some reason this one is just giving it to me. I don't know if its something obvious or not (i am just a beginner), but i just can't seem to get my totals to come up correctly. I did some googling but everything i found was at a much higher level than what I'm doing. I tried doing each total individually to see what happened and if i just do one it comes out correctly. If i had a second total to be added to the first, its like its just giving me the second total by itself and when i add the third it just adds the second and third totals. Its frustrating the heck out of me because I'm getting most of the harder tasks. Anyways please help me anyway you can, and try and be easy on me, I'm just learning. Thank you.

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
  // Lab05P2.cpp - registration for training courses
// Created by Chris Sykes on 2-27-14
// This program calculates total course fee for training courses

#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
    double numRegMorning = 0.0;
    double numRegAfternoon = 0.0;
    double numRegWholeDay = 0.0;
    double totFeeMorning = 0.0;
    double totFeeAfternoon = 0.0;
    double totFeeWholeDay = 0.0;
    double totFee = 0.0;
    
    cout << "Number of people registered for morning only: ";
    cin >> numRegMorning;
    cout << "Number of people registered for afternoon only: ";
    cin >> numRegMorning;
    cout << "Number of people registered for whole day: ";
    cin >> numRegWholeDay;
    
    totFeeMorning = numRegMorning * 90;
    totFeeAfternoon = numRegAfternoon * 110;
    totFeeWholeDay = numRegWholeDay * 160;
    totFee = totFeeMorning + totFeeAfternoon + totFeeWholeDay;
    cout << "Total course fee: " << totFee << endl;
    
    system("pause");
    return 0;
}
Should lines 20 and 22 be identical?
fire thank you so much. i knew it was going to be something simple like that (face palm). I swear i looked this thing over again and again. Guess my professor was right when she said the best way to find simple mistakes is just have someone take a quick look over your code. Thanks again.
I think cire found your problem. You're never asking the user to input numRegAfternoon, so you're basically overwriting numRegMorning.

The rest of your code seems fine.
May I ask why you have the number of people as doubles? Do you consider something missing a limb to be part of a person? Also your prices are all integers 90, 110, 160 so you could make everything an integer for a little bit more precision.
Still new to this, so even your question is a bit puzzling to me. I have the people as doubles because thats how i remember doing it in class. Is it unnecessary or should i be doing something else instead? Im not to sure what the missing limb question means, I'm guessing it pertains to using people as doubles? And as far as the last part what other number were you saying to make an integer? Please feel free to get back to me i really appreciate the help and knowledge. Thank you
Basically you want to use double when you have decimal values eg (1.27) and integer when they are integer values eg (5). Doubles are not as precise as integers so you could be off by a little bit (prob won't affect this program) because of the way that they are stored.


Since the price are integers 90 , 110 , 160 you will always have a total price that is an integer also so there will be no change like $1.50 or something so you would be best writing it as all integers:

1
2
3
4
5
6
7
    int numRegMorning = 0;
    int numRegAfternoon = 0;
    int numRegWholeDay = 0;
    int totFeeMorning = 0;
    int totFeeAfternoon = 0;
    int totFeeWholeDay = 0;
    int totFee = 0;


http://www.cplusplus.com/doc/tutorial/variables/
Wow definitely helping me out there bud. So say i did need decimals for pricing but also integers for people. Could i do both like this

int numRegMorning = 0;
int numRegAfternoon = 0;
int numRegWholeDay = 0;
double totFeeMorning = 0;
double totFeeAfternoon = 0;
double totFeeWholeDay = 0;
double totFee = 0;

or would i have to separate them? Anyways thanks a ton man, i'll definitely be back for future help
Hey blit, if your still on, heres another one I'm having a problem with a little farther down. I have to write a program that ill show how much of a child tax credit certain households will receive. If income is under 70000 they receive 1000 per child. If the income is above 70000 they cannot claim more than 4500 regardless of amount of kids. I have everything else coded and done but i don't know how to limit the tax credit for those over 70000. How do i say 1000 per child up to 4500. Im a little frustrated because i don't know if I'm just missing something i should already know. Im pretty sure theres not anything else wrong, just don't know how to make that command happen. Ive looked through our notes, handouts, and slides pertaining to this lesson and still nothing. Maybe I'm over thinking it and theres more logical way to calculate this. Again any help is much appreciated.

// File: Lesson08P1.cpp
// Created by Chris Sykes on 2-27-14
// This program ucalculates and displays the total child tax credit households can clam based on income.

#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
// Declare variables
double totIncome = 0.0;
double totChild = 0.0;
double taxCredit = 0.0;

// Request input
cout << "Total income:";
cin >> totIncome;
cout << "Amount of children:";
cin >> totChild;

// Check about graduation and display message
if (totIncome < 70000)
{
taxCredit = totChild * 1000;
cout << taxCredit << endl;
}
else
// This is where I'm having the trouble *************************
{
taxCredit = totChild * 1000;
cout << taxCredit << endl;
}

system("pause");
return 0;
}
Yes you can mix and match variable types.
Though keep in mind double you should assign a decimal after the integer (0 = 0.0)value. It will implicitly cast it if you forget though.

You could do a few different things.

You could
1) multiply the children by 1000 and check if that is greater than 4500 and if so assign it a value of 4500 or
2) check if there are 5 or more children if so assign 4500 to be the credit.
3) if children less than 5 value is 4500 else 100 * children


here is option 3:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
else
// This is where I'm having the trouble *************************
{
taxCredit = totChild * 1000;
cout << taxCredit << endl;
}


//replaced with:
else if(totChild < 5) //4 or less children it will have already checked
//less than 70000 and failed
//so that would implicitly mean it has 70000 or more
//so you don't need to check that again
{
    taxCredit = totChild * 1000.0; //.0 casts to integer
    //int * int = int
    //float/double * int = float/double
}
else //more than 70000 and more than 4 children
{
    taxCredit = 4500.0;
}

Thanks a million Blit. This definitely helped me out big time. Im sure the more i stay with it the simpler it will come to me. If you don't mind me asking, whats your profession? Well i guess i don't mean whats your profession, im just a bit curious about how you know all this. I just stumbled upon this class but I'm really enjoying it and want to learn more and more about it. Again bud thanks for everything, I'm sure ill have more questions.
Just comes with practice. Only took two semesters of it at the university.
Topic archived. No new replies allowed.