Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
Hello all experienced more than me people! :D I have a very urgent favor to ask of you all. I have an intro to C++ class online and I am doing a project, which is very confusing. It is about functions and for some reason, the class program or XCode doesn't want to seem to wanna build some obvious things. For example, here is a very very basic version of my program (without anything related to functions at all, I like to build from the ground up :D):

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
double breakfast = 5.50;
double bDiscount = .10;
double quanBreak = 10;
double discountAmt, subTotal, totalOne, breakfastTotal;
int breakfastMeals;

cout << "Enter the number of Breakfast Meals ordered: ";

cin >> breakfastMeals >> endl;

totalOne = breakfastMeals * breakfast;
if (breakfastMeals >= quanBreak);
discountAmt = totalOne / bDiscount;
subTotal = totalOne - discountAmt;
breakfastTotal = subtotal * .10;
else
breakfastTotal = totalOne * .10;

cout << "Your Total Is: " << breakfastTotal << endl;

return 0;
}

I get a build error on the first "cin >>" line!!! WTF? It's never done this before and I have no clue what it is. But like I always say, two sets of eyes are better than one. Obviously, I'm missing something . . . Any suggestions? And please, I deserve to be called a n00b for this one prolly so feel free.
 

uaaerospace

macrumors 6502
Feb 15, 2005
396
0
Alabama
Try removing the ">> endl" from the cin statement.

Also, keep program scope in mind when writing if statements. The proper structure for an if statement is

if (argument)
{
statement;
statement;
}

You have to use the {}'s unless you just have one statement. In your code, you have 3 statements, so the {}'s are needed.


Also...there is no if-else statement in c++. Use another if statement.

haha...Also. Keep in mind that coding in c++ is usually if not always case sensitive. You may wish to check your use of subtotal vs. subTotal. You are using a good convention (camelCase), just stick with it.
 

deputy_doofy

macrumors 65816
Sep 11, 2002
1,466
410
uaaerospace said:
Try removing the ">> endl" from the cin statement.

Yes. That's exactly it.
endl means ENDLINE, (which basically puts the next output on the next line) and you would only use that for output.
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
uaaerospace said:
Try removing the ">> endl" from the cin statement.

Also, keep program scope in mind when writing if statements. The proper structure for an if statement is

if (argument)
{
statement;
statement;
}

You have to use the {}'s unless you just have one statement. In your code, you have 3 statements, so the {}'s are needed.


Also...there is no if-else statement in c++. Use another if statement.

Thanks for the help, I can't believe I didn't see that. What do you mean there is no if-else statement in C++? Don't think I'm calling you a liar, but I've been using If-Else statements for weeks now . . . unless I am reading your statement wrong. Geez, what a day lol.
 

deputy_doofy

macrumors 65816
Sep 11, 2002
1,466
410
sixstorm said:
Thanks for the help, I can't believe I didn't see that. What do you mean there is no if-else statement in C++? Don't think I'm calling you a liar, but I've been using If-Else statements for weeks now . . . unless I am reading your statement wrong. Geez, what a day lol.

There IS an if (then) statement. Well, there's no THEN...
if (comparison)
{
// Do stuff and things.
// Do more stuff and things.
}
else
{
// Do completely different stuff and things.
// Do MORE completely different stuff and things.
}
 

uaaerospace

macrumors 6502
Feb 15, 2005
396
0
Alabama
sixstorm said:
What do you mean there is no if-else statement in C++? Don't think I'm calling you a liar, but I've been using If-Else statements for weeks now . . . unless I am reading your statement wrong.
hmm, well, it compiles fine with the if/else command in xCode, however the text I have on c++ mentions no such command. Also, when taking c++ classes, we always used if statements. Perhaps it's just personal preference of my professors. Oh well, I learn something new every day....this time from a self proclaimed "noob" :p :D
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
sixstorm said:
Thanks for the help, I can't believe I didn't see that. What do you mean there is no if-else statement in C++? Don't think I'm calling you a liar, but I've been using If-Else statements for weeks now . . . unless I am reading your statement wrong. Geez, what a day lol.

The way you have it, the statement discountAmt = totalOne / bDiscount; is executed for the if. The semicolon doesn't belong on the statement with the if.

The other statements, had it compiled, except for the else would be executed without condition.

When confused, use the braces {} to explicitly declare the block.
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
Thanks a lot for the help. I played and tinkered for a little bit and finally got it to work. I do have to take out a lot of "//" in my program (there's a lot more to the program lol) and add my function. I'm sure I'll be back here soon with more Q's. Thanks again everybody.
 

Heath

macrumors regular
Aug 19, 2005
133
0
Canada
Another thing.

You might want to revisit that if statement again.

You had:
if (breakfastMeals >= quanBreak);

This will not execute any conditional logic because of the semicolon at the end.
It will process the if statement, then merrily execute code that immediately follows, regardless of the outcome of that comparison.

(At least this was the case the last time I touched C++, ymmv).
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
Here we go again. I'm trying to use a function now . . . here's the full code:

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

double calcDiscount (int quantity, double costEach, double discountPerCent, int orderMoreThan);

int main()
{
double breakfast = 5.50;
double bDiscount = .10;
double quanBreak = 10;
double breakfastTotal;
int breakfastMeals;

cout << "Enter the number of Breakfast Meals ordered: ";

cin >> breakfastMeals;

breakfastTotal = calcDiscount (breakfastMeals, breakfast, bDiscount, quanBreak);
cout << "Your total is: "<< breakfastTotal << endl;

}

double calcDiscount (int quantity, double eachCost, double discountPerCent, int orderMoreThan);

{
double discountAmt, totalOne, subTotal;

totalOne = quantity * costEach;
if (quantity >= orderMoreThan)
{
discountAmt = totalOne * bDiscount;
subTotal = totalOne - discountAmt;
}
else
{
subTotal = totalOne * .10;
}
return subTotal;

}

It's giving me errors on subTotal and totalOne with "no type" (whatever that means) and a few other things. Any ideas?
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
sixstorm said:
Here we go again. I'm trying to use a function now . . . here's the full code:



It's giving me errors on subTotal and totalOne with "no type" (whatever that means) and a few other things. Any ideas?

Watch where you put your semicolons.

Note the difference between your definition of main() and your definition of calcDiscount().
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
bousozoku said:
Watch where you put your semicolons.

Note the difference between your definition of main() and your definition of calcDiscount().

I've got both definitions in {}s, so that shouldn't be a problem. BTW, I'm using a remote terminal (via OSX terminal, which is a PITA) and it's giving me different errors than XCode is. XCode is a bit easier to work with.
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
sixstorm said:
I've got both definitions in {}s, so that shouldn't be a problem. BTW, I'm using a remote terminal (via OSX terminal, which is a PITA) and it's giving me different errors than XCode is. XCode is a bit easier to work with.

Let me put it plainly: you have a semicolon between the closing parenthesis and the opening brace.
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
bousozoku said:
Let me put it plainly: you have a semicolon between the closing parenthesis and the opening brace.

If I'm not being a PITA for ya, could you explain why I wouldn't put it there? Just trying to learn . . .
 

Heath

macrumors regular
Aug 19, 2005
133
0
Canada
Better yet,

Try to explain why you WOULD put it there.

double calcDiscount (int quantity, double eachCost, double discountPerCent, int orderMoreThan);

The semicolon is usually an indication to the compiler that you're done whatever it is you are going to do in that statement.
So you're telling the compiler, 'Hey I'm done defining my function!' and it goes 'alright.. an empty function.. gotcha.. what's the rest of the code for then?' and proceeds to spew errors at you that make no sense :)
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
Heath said:
Try to explain why you WOULD put it there.

double calcDiscount (int quantity, double eachCost, double discountPerCent, int orderMoreThan);

The semicolon is usually an indication to the compiler that you're done whatever it is you are going to do in that statement.
So you're telling the compiler, 'Hey I'm done defining my function!' and it goes 'alright.. an empty function.. gotcha.. what's the rest of the code for then?' and proceeds to spew errors at you that make no sense :)

Okee dokee, that makes sense. Now tell me this. Take a look at my identifiers, my function call and my function definition header. Why am I still getting an errors about "assignment 'int' to 'double'"? If in a function call, it makes the temp memory cells from left to right, it shouldn't have a problem defining those temp memory cells for this.

EDIT: Guess I should give you some code huh?

int quanBreak = 10;
int breakfastMeals;
double bDiscount = .10;
double breakfast = 5.50;

double calcDiscount (int quantity, double costEach, double discountPerCent, int orderMoreThan)
 

Heath

macrumors regular
Aug 19, 2005
133
0
Canada
It looks like you're trying to stuff a double into an int, not the other way around.


double calcDiscount (int quantity, double costEach, double discountPerCent, int orderMoreThan);

double quanBreak = 10; <-- this won't fit into an int even though it looks like it should.

breakfastTotal = calcDiscount (breakfastMeals, breakfast, bDiscount, quanBreak);

You are correct in that the ints can be implicitly converted to doubles, but the last argument you are passing in a double into an int, and the compiler will complain about that one since it can result in the loss of information.
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
Heath said:
It looks like you're trying to stuff a double into an int, not the other way around.


double calcDiscount (int quantity, double costEach, double discountPerCent, int orderMoreThan);

double quanBreak = 10; <-- this won't fit into an int even though it looks like it should.

breakfastTotal = calcDiscount (breakfastMeals, breakfast, bDiscount, quanBreak);

You are correct in that the ints can be implicitly converted to doubles, but the last argument you are passing in a double into an int, and the compiler will complain about that one since it can result in the loss of information.

I changed the "double quanBreak" into "int quanBreak" (that's what it's supposed to be, but I'm still getting errors on my function call. Why won't 10 go into an int like that?
 

ifjake

macrumors 6502a
Jan 19, 2004
562
1
I'm also in a beginners C++ course and am looking at some of your stuff and comparing it to how I'm being taught.

I'm actually interested in your #include statements and such.

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;


We use <iostream.h> which means that the std::cout stuff and
using namespace std; aren't needed. But I feel like we're using outdated stuff. If this is all it takes to move to newer stuff I might do that on my own. But is <iomanip> needed to state using namespace std;?

Are there any similar steps that need to be taken when moving from <fstream.h> to <fstream>?
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
ifjake said:
I'm also in a beginners C++ course and am looking at some of your stuff and comparing it to how I'm being taught.

I'm actually interested in your #include statements and such.

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;


We use <iostream.h> which means that the std::cout stuff and
using namespace std; aren't needed. But I feel like we're using outdated stuff. If this is all it takes to move to newer stuff I might do that on my own. But is <iomanip> needed to state using namespace std;?

You're right. You're using C++ prior to the ISO and ANSI standards.
 

ifjake

macrumors 6502a
Jan 19, 2004
562
1
bousozoku said:
You're right. You're using C++ prior to the ISO and ANSI standards.

What do I need to do to get stuff to work right with these new standards?
 

YoNeX

macrumors regular
Apr 29, 2005
141
0
Ahh yes memories of the introduction C++ class. My criticism is why do you need the iomanip and the cmath libs? From the code, I don't see any any purpose for these libs from the code given. Another pointer I can give you is try to use abbreviations for your function names. You are prone to make less mistakes if you use abreviations. For example instead of entering in quantity, use qty. Instead of breakfast, you can use bfast. These small little thing will add up, plus you avoid line wrapping (or having long horizontal code). Oh yeah, forgot, don't need to take my advice, many people have different style of coding. Use what you like (or use based on the school's coding style).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.