Free2Code
The majority of forums are now only available as archives, which means posting/editing is disabled.

The Anything and Everything forum is still open.
 
Time: 2013-05-23, 10:28am
C++ programs
Subject: C++ programs  ·  Posted: 2003-10-22, 06:54am
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.
 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ programs  ·  Posted: 2003-10-22, 07:18am
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.
 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ programs  ·  Posted: 2003-10-22, 07:47am
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?

 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ programs  ·  Posted: 2003-10-22, 08:28am
Rank: ? (4827)
Member #: 3416
here's your code with some comments pointing out syntax errors:

Code:
  1. #include <iostream>
  2. int main ()
  3. {
  4.   // declare variables
  5.   double connect = 1.99;
  6.   float first = 3;
  7.   float two = 2;  //------this is kind of a silly declaration
  8.   double add = .45;
  9.   int min  //----------forgot the semicolon here
  10.   // declare variables for calculations
  11.   float remain = min - first;  //------min has no value at this point
  12.   double cost = connect + two + (remain * add);
  13.   // process
  14.   float min;  //---------already declared as int min -- you'll need to use a different name if these are actually different variables
  15.   cout>> "Please enter the number of minutes you spoke: ";  //-----use << not >>
  16.   cin << min;  //-----use >> not <<
  17.   // conditional statement stating to either enter a valid number or
  18.   calculate the cost of the call //-----try a // at the beginning of this line
  19.   // and print the calculation
  20.   if min <= 0 {
  21.     cout >> "The number you entered is not valid. Please enter a positive  //----- use << not >>
  22.     number of minutes: ";  //---this needs to be part of the previous line.  
  23.     cin; << min; //----- no ; after cin, and use >> not <<
  24.   else  //-----need to end the if part of this if-else with a } before you can say this
  25.     cout >> "The cost of your call is " >> cost; //----- use << not >>
  26.   }  //-----either get rid of this or put a { after else for it to match to.
  27.   // end program
  28.   return 0;
  29. }


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
 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ programs  ·  Posted: 2003-10-22, 12:33pm
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
 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ programs  ·  Posted: 2003-10-22, 01:59pm
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

 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ programs  ·  Posted: 2003-10-22, 02:01pm
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.

 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ programs  ·  Posted: 2003-10-22, 02:09pm
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;
}

 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ programs  ·  Posted: 2003-10-23, 04:10am
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
 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ programs  ·  Posted: 2003-10-23, 12:57pm
Rank: Unregistered
OK Thank you

I have it all straightened out now. Thank you very much for your help.

 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ programs  ·  Posted: 2003-10-25, 07:12am
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;
}


 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

Penguino AVR

Want to learn about robotics or microcontrollers?
Check out the Penguino AVR from our friends at
Icy Labs