The majority of forums are now only available as archives, which means posting/editing is disabled.
The Anything and Everything forum is still open.
The Anything and Everything forum is still open.
C++ programs
|
|||
|
Rank: Unregistered
|
hi I am trying to make a program that calculates a phone call but am confused as to what and where to delcare my variables.
#include <iostream> int main () { // declare variables double connect = 1.99; float first = 3; float two = 2; double add = .45; int min // declare variables for calculations float remain = min - first; double cost = connect + two + (remain * add); // process float min; cout>> "Please enter the number of minutes you spoke: "; cin << min; // conditional statement stating to either enter a valid number or calculate the cost of the call // and print the calculation if min <= 0 { cout >> "The number you entered is not valid. Please enter a positive number of minutes: "; cin; << min; else cout >> "The cost of your call is " >> cost; } // end program return 0; } what am I doing wrong? Any help would be greatly appreciated. |
||
|
|||
|
|||
|
Rank: ? (14)
Member #: 10544 |
int min;
... if(min <= 0) { cout >> "The number you entered is not valid. Please enter a positive number of minutes: "; cin << min; } else { cout >> "The cost of your call is " >> cost; } what exactly is going wrong?
I will not be defeated because I believe in me.
|
||
|
|||
|
|||
|
Rank: Unregistered
|
I get errors from lines 20-32.
(14): error C2144: syntax error : 'float' should be preceded by ';' (18): error C2371: 'min' : redefinition; different basic types (19): error C2065: 'cout' : undeclared identifier (19): error C2296: '>>' : illegal, left operand has type ''unknown-type'' (19): error C2297: '>>' : illegal, right operand has type 'char [47]' (20): error C2065: 'cin' : undeclared identifier (20): error C2296: '<<' : illegal, left operand has type ''unknown-type'' (23): error C2065: 'calculate' : undeclared identifier (23): error C2146: syntax error : missing ';' before identifier 'the' (23): error C2065: 'the' : undeclared identifier (23): error C2146: syntax error : missing ';' before identifier 'cost' (23): error C2146: syntax error : missing ';' before identifier 'of' (23): error C2065: 'of' : undeclared identifier (23): error C2146: syntax error : missing ';' before identifier 'the' (25): error C2146: syntax error : missing ';' before identifier 'call' (25): error C2065: 'call' : undeclared identifier (25): error C2143: syntax error : missing ';' before 'if' (26): error C2001: newline in constant (27): error C2296: '>>' : illegal, left operand has type ''unknown-type'' (27): error C2297: '>>' : illegal, right operand has type 'char [62]' (27): error C2146: syntax error : missing ';' before identifier 'number' (27): error C2065: 'number' : undeclared identifier (27): error C2146: syntax error : missing ';' before identifier 'of' (27): error C2146: syntax error : missing ';' before identifier 'minutes' (28): error C2296: '<<' : illegal, left operand has type ''unknown-type'' (28): error C2146: syntax error : missing ';' before identifier 'cin' (27): error C2001: newline in constant (32): error C2297: '>>' : illegal, right operand has type 'char [26]' (32): error C2296: '>>' : illegal, left operand has type ''unknown-type'' Ok that is what I get when I try to run the program. what am i doing wrong cause Obviously its alot. especially line 23 and 27. Anyone can help? |
||
|
|||
|
|||
|
Rank: ? (4827)
Member #: 3416 |
here's your code with some comments pointing out syntax errors:
Code:
if you have any specific questions from that, i can explain further.
my mind is like a steel trap! it only hangs on to the big stuff. visit my forums at track7.org
|
||
|
|||
|
|||
|
Rank: ? (614)
Member #: 9832 |
int min;
float remain = min -first; problem: min has yet no value (or random value). float min; ... cin<<min; problem: cin doesn't know which min is supposed to be used here. if(min<=0) problem: same thing. You need to give different names to those variables. Also, variable needs to have some value before it can be used in calculations.
Chaos reigns within - Reflect, repent, and reboot - Order shall return
|
||
|
|||
|
|||
|
Rank: Unregistered
|
Thanks for your help. I have gotten it down to one error:
no match for 'ostream & >> double &' Here is what my code now looks like: #include <iostream> int main () { // declare variables double connect = 1.99; float first = 3; float two = 2; //------this is kind of a silly declaration double add = .45; // process float min; cout<< "Please enter the number of minutes you spoke: "; cin >> min; // declare variables for calculations float remain = min - first; //------min has no value at this point double cost = connect + two + (remain * add); // conditional statement stating to either enter a valid number or // calculate the cost of the call // and print the calculation if (min <= 0) { cout << "The number you entered is not valid. Please enter a positive number of minutes: "; cin >> min; //----- no ; after cin, and use >> not << } else { cout << "The cost of your call is " >> cost; } // end program return 0; } Thanks |
||
|
|||
|
|||
|
Rank: Unregistered
|
i think i got it
i changed the last cout to cout << "The cost of your call is" << cost; I got zero errors. I just don't know if it will run properly yet. |
||
|
|||
|
|||
|
Rank: Unregistered
|
OK here is what I have with no compile errors, but it won't run properly. I think it is not making any calculation.
#include <iostream> int main () { // declare variables double connect = 1.99; float first = 3.0; float two = 2.0; double add = .45; // process float min; cout<< "Please enter the number of minutes you spoke: "; cin >> min; // conditional statement stating to either enter a valid number or // calculate the cost of the call // and print the calculation if (min <= 0) { cout << "The number you entered is not valid. Please enter a positive number of minutes: "; cin >> min; } else { // declare variables for calculations float remain = min - first; double cost = connect + two + (remain * add); cout << "The cost of your call is " << cost; } // end program return 0; } |
||
|
|||
|
|||
|
Rank: ? (4827)
Member #: 3416 |
looks like it should work as long as you enter a positive number of minutes. you may want to change your if (min <= 0) block to a while (min <= 0), and then take the else away from the else block. that way you will keep asking for number of minutes until they enter a positive number, and then you'll still calculate it. the way you have it now, it checks for a negative number and if it finds one it asks for it again, but then it stops.
my mind is like a steel trap! it only hangs on to the big stuff. visit my forums at track7.org
|
||
|
|||
|
|||
|
Rank: Unregistered
|
OK Thank you
I have it all straightened out now. Thank you very much for your help. |
||
|
|||
|
|||
|
Rank: ? (6)
Member #: 10761 |
#include <stdio.h> #include <iostream.h> int main () { // declare variables double connect = 1.99; int first = 3; int two = 2; double add = .45; int min = 0.0; int remain = 0.0; double cost = 0.0; label: cout<< "Please enter the number of minutes you spoke: "; cin >> min; remain = min - first; cost = connect + two + (remain * add); // process // conditional statement stating to either enter a valid number or // and print the calculation if (min <= 0 ) { cout << "The number you entered is not valid "<<endl; goto label; } else { cout << "The cost of your call is " << cost; } // end program return 0; } |
||
|
Please login or register to post a reply.
