I am trying to create an expression evaluator to expand my C++ knowledge. It is supposed to evaluate 5(x+7)-2. Here is the code:

#include <iostream>
#include <stdio.h>
#include <time.h>

using namespace std;

time_t now, later;

void sleep(int delay)
{
 now=time(NULL);
 later=now+delay;
 while(now<=later)now=time(NULL);
}

int main(void){
    //It finds an error here  
   int x, 5, 7, 2, total;
   printf("Hello. This program will evaluate the expression 5(x+7)-2. \nEnter the value for x:");
   //And here
   total= 5(x+7)-2;
   cin >> x;
   cout << total << endl;
   getchar();
   return 0;}

I know it contains many errors; can someone tell me what I should do?

Thanks a lot!!!

Edited 6 Years Ago by arithehun: Simple mistake/

#include <iostream>

using namespace std;

int main()
{
   int x = 0, 
	   Total = 0; 
   /* You must make a varaible a, b, c to hold the value unless they are constant */

   printf( "Hello. This program will evaluate the expression 5(x+7)-2. \nEnter the value for x: " );
   
   cin >> x;
   /* Take in the number before doing the equation */

   Total = ( 5 * ( x + 7 ) - 2 );
   /* Use multiplication operator *, use parentheses to give the absolute result of the equation to total 
   Make sure to research the methods for arithmic operations */

   cout << Total << endl;

   system( "PAUSE" );
   /* uses the command prompt Pause command to wait for user to press a key */

   return 0;
}

shoop

I am trying to create an expression evaluator to expand my C++ knowledge. It is supposed to evaluate 5(x+7)-2. Here is the code:

#include <iostream>
#include <stdio.h>
#include <time.h>

using namespace std;

time_t now, later;

void sleep(int delay)
{
 now=time(NULL);
 later=now+delay;
 while(now<=later)now=time(NULL);
}

int main(void){
    //It finds an error here  
   int x, 5, 7, 2, total;
   printf("Hello. This program will evaluate the expression 5(x+7)-2. \nEnter the value for x:");
   //And here
   total= 5(x+7)-2;
   cin >> x;
   cout << total << endl;
   getchar();
   return 0;}

I know it contains many errors; can someone tell me what I should do?

Thanks a lot!!!

For starters numeric literals (i.e. 5, 7, 2) are not valid variable names and are not required to be declared. Line 18 should be simply int x=0, total=0; Second, if you want to use user input in a calculation you need to get the input before you do the calculation. Switch lines 21 and 22.
Have you read any tutorials? This is all very basic stuff that you shold know if you've done any studying at all.

Edited 6 Years Ago by Fbody: n/a

Comments
Indeed - completing the assignment is some way off with basic mistakes like this still to be resolved.
#include <iostream>

using namespace std;

int main()
{
   int x = 0, 
	   Total = 0; 
   /* You must make a varaible a, b, c to hold the value unless they are constant */

   printf( "Hello. This program will evaluate the expression 5(x+7)-2. \nEnter the value for x: " );
   
   cin >> x;
   /* Take in the number before doing the equation */

   Total = ( 5 * ( x + 7 ) - 2 );
   /* Use multiplication operator *, use parentheses to give the absolute result of the equation to total 
   Make sure to research the methods for arithmic operations */

   cout << Total << endl;

   system( "PAUSE" );
   /* uses the command prompt Pause command to wait for user to press a key */

   return 0;
}

shoop

Thank you so much. No, I do not know very much C++, but that is subject to change. I am only somewhat fluent in ActionScript 2.0, which, even for a 13 year old, may be a little behind. but thank you. And I see my mistake clear as ice.

This question has already been answered. Start a new discussion instead.