I cant find anything wrong with my program, any suggestions?

#include <stdio.h>
#include "simpio.h"
#include "strlib.h"
#include "random.h"

int main()
{
int s, r, h, w, l, t, shape;
string box;
string cube;
string cylinder;

printf("Please enter the shape; ");
shape = GetLine();


                  switch(shape)
    {

         case 1:cube();break;
         case 2:box();break;
		 case 3:cylinder();break;
	}

	if (StringEqual(shape, "cube"));
	{
	printf("Enter side: ");
	s = GetInteger();

	t = s*s*s;
	printf("The volume of a cube with side=%d is: %d",s , t);
	}
	if (StringEqual(shape, "box"));
	{
	printf("Enter width: ");
	w = GetInteger();

	printf("Enter height: ");
	h = GetInteger();

	printf("Enter length: ");
	l = GetInteger();

	t = w*l*h;
	printf("The volume of a box with width=%d, height=%d and length%d is: %d",w ,h ,l, t);
	}
	if (StringEqual(shape, "cylinder"));
	{
	printf("Enter radius: ");
	r = GetInteger();

	printf("Enter height: ");
	h = GetInteger();

	t = 3.14*r*r*h;
	printf("The volume of a cylinder with radius=%d and height=%d is: ",r ,h , t);
	}
system("pause");
}
1>        c:\program files (x86)\microsoft visual c++ 2008\vc\include\stdio.h(324) : see declaration of 'sscanf'
1>c:\users\franz sauer\desktop\programs\volumes\volumes\volumes.cpp(18) : error C2450: switch expression of type 'string' is illegal
1>        Integral expression required
1>c:\volumes.cpp(21) : error C2064: term does not evaluate to a function taking 0 arguments
1>c:\volumes.cpp(22) : error C2064: term does not evaluate to a function taking 0 arguments
1>c:\volumes.cpp(23) : error C2064: term does not evaluate to a function taking 0 arguments

Dani AI

Generated

The compiler errors show three separate root problems that need to be fixed together: switching on a non-integral type, calling variables as if they were functions, and logic being bypassed by stray semicolons. The message "switch expression of type 'string' is illegal" (C2450) means a switch must use an integral type (int/char/enum). The "term does not evaluate to a function" errors (C2064) come from declaring identifiers like cube, box, cylinder as variables and then writing cube(); those names must be functions (or the variable declarations removed/renamed). and @bhi correctly pointed at those problems, and was right to flag the non-standard helpers (GetLine, GetInteger, StringEqual) — prefer standard input routines or the library that actually provides them.

Concrete, practical fixes:

  • Decide whether the UI is a numeric menu or a typed name. Use an integer for a menu (then switch works), or use a string and strcmp()/if-else (switch cannot handle strings).
  • Remove any variable whose name collides with a function name.
  • Remove stray semicolons after if conditions (if(cond); { ... } runs unconditionally).
  • Use the correct types and printf formats: use double for cylinder volume and print with %f (or %.2f), or cast carefully when using ints.
  • Add a default case to the switch and validate input (check return values of fgets/sscanf or scanf).

Example patterns (minimal):

/* menu -> switch on integer */
int choice;
char buf[64];
fgets(buf, sizeof buf, stdin);
sscanf(buf, "%d", &choice);

switch (choice) {
  case 1: /* cube */ break;
  case 2: /* box */ break;
  case 3: /* cylinder */ break;
  default: printf("Unknown choice\n"); break;
}
/* string name -> if/else + strcmp */
char shape[32];
fgets(shape, sizeof shape, stdin);
shape[strcspn(shape, "\n")] = '\0'; /* trim newline */

if (strcmp(shape, "cube") == 0) {
  /* read side and compute side*side*side */
}
else if (strcmp(shape, "cylinder") == 0) {
  /* read r,h and compute 3.14159*r*r*h into a double and print with %.2f */
}

Checklist before recompiling: remove conflicting variable declarations, delete the extra semicolons after if, fix printf format strings to match arguments, and decide whether to compile as C (.c) or C++ (.cpp) depending on the libraries in use.

Recommended Answers

All 5 Replies

shape = GetLine(); don't use GetLine(), use something like scanf()

you don't have a default: statement for your switch statement and it looks for all the world like you're trying to call variables as functions in your case statement.

string box;
case 2:box();break;

after that it looks like you're trying to compare an integer value with a string... don't use StringEqual() either.. use something like strcmp()...

int s, r, h, w, l, t, shape;
if (StringEqual(shape, "cube"));

>>don't use GetLine(), use something like scanf()

Why dont you elaborate more on why he shouldnt use getline(). I believe even if he followed your advice and changed getline() to scanf() he wouldnt have a clue as to why he did it

>I cant find anything wrong with my program, any suggestions?

Then, it would appears that most of the necessary steps to correct your program would be over your head. This is not a reason for discouragement, it just says that you need to learn some more.
Starting with switch, read here some examples.

GetLine(), GetInteger(), StringIquals() are non standard C functions. Learning C without learning first the standard C functions is a little backwards.
Instead of GetLine() learn how to use fgets(); some examples here.
For obtainning an integer use the combination of two functions. fgets() to read a string, and sscanf() to convert that string to an integer. More about sscanf() .
Instead of StringIquals() learn or strncmp().. I am sure you can find plenty more examples searching the Internet, if you need more to draw an understanding.

And don't use scanf() to read any thing from the standard input until you know the consequence of it. That's why I find this previous comment a bad advise.

shape = GetLine(); don't use GetLine(), use something like scanf()

I stand corrected fgets() is the smarter way to go.

What exactly is the data type of variable shape?

You have declared it as an int and used it as an integer in the switch.

But later you have used :

if (StringEqual(shape, "cube"));

Use it as any one data type.

Also there is no semi-colon at the end of that statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.