4 Replies - 1872 Views - Last Post: 27 February 2009 - 02:53 PM Rate Topic: -----

#1 etotheo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 27-February 09

Errors using Visual C++ for simple C code

Post icon  Posted 27 February 2009 - 02:29 PM

Trying to write a C code for simple commands (add, subtract, ...).
As coding goes, I'm green like the hills of Ireland.

I'm using Visual C++ and it's giving me 40 some errors :crazy:

Most are syntax errors and undeclared identifiers.

#include <stdio.h> /* standard input/output header */
#pragma warning(disable: 4996) /* disables Visual C++ warnings regarding C coding */

/* funtion main begins program execution */
int main(){

	/* general description to user of the program */
	printf("This program is a simple command interface.\n");
	printf("Command codes are as follows,\n");
	printf("  E: Enter Data\n");
	printf("  A: Add\n");
	printf("  S: Subtract\n");
	printf("  M: Multiply\n");
	printf("  D: Divide\n");
	printf("Enter EOF charachter to end program\n\n");

	int x; /* integer to be read from user */
	int y; /* integer to be read from user */
	int z; /* result to be printed */
	int command;

	/* loop until user types end-of-file key sequence */
	while ( ( command = getchar() ) != EOF ){

		z = 0;

		/* prompts user for input command */
		printf("Enter command: ");

		/* determine which command was input */
		switch (command){ /*switch nested within the while loop */

			case 'E':
				printf("Enter two integers: ", x, y); /* prompts user to enter integers */
				scanf("%d %d", &x, &y); /* read two integers */
				break; /* exit switch */
			
			case 'A':
				z = x + y;
				printf("sum = %d\n", z);
				break; /* exit switch */

			case 'S':
				z = x - y;
				printf("difference = %d\n", z);
				break; /* exit switch */
			
			case 'M':
				z = x * y;
				printf("product = %d\n", z);
				break; /* exit switch */
			
			case 'D':
				z = x / y;
				printf("quotient = %d\n", z);
				break; /* exit switch */

			case '\n': /* ignores newlines */
			case '\t': /* ignores tabs */
			case ' ': /* ignores spaces */
				break; /* exit switch */

			default:
				printf("error in command code");
				break; /* optional, will exit switch anyway */

		} /* end switch */

	} /* end while loop */

	return 0; /* indicate program ended successfully */

} /* end function main */



Here's a list of the errors,
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(23) : error C2143: syntax error : missing ';' before 'type'
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(24) : error C2143: syntax error : missing ';' before 'type'
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(25) : error C2143: syntax error : missing ';' before 'type'
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(26) : error C2143: syntax error : missing ';' before 'type'
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(29) : error C2065: 'command' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(31) : error C2065: 'z' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(37) : error C2065: 'command' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(40) : error C2065: 'x' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(40) : error C2065: 'y' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(41) : error C2065: 'x' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(41) : error C2065: 'y' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(45) : error C2065: 'x' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(45) : error C2065: 'y' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(45) : error C2065: 'z' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(45) : error C2106: '=' : left operand must be l-value
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(46) : error C2065: 'z' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(50) : error C2065: 'x' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(50) : error C2065: 'y' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(50) : error C2065: 'z' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(50) : error C2106: '=' : left operand must be l-value
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(51) : error C2065: 'z' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(55) : error C2065: 'x' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(55) : error C2065: 'y' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(55) : error C2065: 'z' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(55) : error C2106: '=' : left operand must be l-value
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(56) : error C2065: 'z' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(60) : error C2065: 'x' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(60) : error C2065: 'y' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(60) : error C2065: 'z' : undeclared identifier
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(60) : error C2106: '=' : left operand must be l-value
1>c:\users\evan\documents\visual studio 2008\projects\exercise 5\exercise 5\ex5.c(61) : error C2065: 'z' : undeclared identifier

This post has been edited by etotheo: 27 February 2009 - 02:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Errors using Visual C++ for simple C code

#2 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Errors using Visual C++ for simple C code

Posted 27 February 2009 - 02:41 PM

You missed a few semi-colons. You have your math backwards.

"x + y = z" should be "z = x + y"

            case 'A':
                z = 0;
                z = x + y;
                printf("sum = %d\n", z);
                break; /* exit switch */

            case 'S':
                z = 0;
                z = x - y; 
                printf("difference = %d\n", z);
                break; /* exit switch */

            case 'M':
                z = 0;
                z = x * y;
                printf("product = %d\n", z);
                break; /* exit switch */

            case 'D':
                z = 0;
                z = x / y;
                printf("quotient = %d\n", z);
                break; /* exit switch */


Welcome, hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 etotheo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 27-February 09

Re: Errors using Visual C++ for simple C code

Posted 27 February 2009 - 02:44 PM

View PostHyper, on 27 Feb, 2009 - 01:41 PM, said:

You missed a few semi-colons. You have your math backwards.

"x + y = z" should be "z = x + y"


Thanks, fixed those... down to 27 errors.

This post has been edited by etotheo: 27 February 2009 - 02:45 PM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,238
  • Joined: 23-August 08

Re: Errors using Visual C++ for simple C code

Posted 27 February 2009 - 02:47 PM

A few things...

The older C standard (pre-C99) states that all variable declarations must occur before any actual code. Unfortunately, Microsoft has not updated to reflect C99. So you move your variable declarations to before the printfs in main.

In your cases, you're missing some semicolons, and you can't do x + y = z, you must do z = x + y;. Operations must occur on the right side of statements. Think of z = x + y; being "set the variable z to be equal to the sum of x and y."
Was This Post Helpful? 0
  • +
  • -

#5 etotheo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 27-February 09

Re: Errors using Visual C++ for simple C code

Posted 27 February 2009 - 02:53 PM

View PostJackOfAllTrades, on 27 Feb, 2009 - 01:47 PM, said:

...move your variable declarations to before the printfs in main.


You nailed it! Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1