Help with some beginner C code!

Moderator
Posts: 1,140
Joined: 2005.07
Post: #31
Ok, if you want to do it that way, with a number based system, you need to replace

switch (shape)

with

switch (shape[0])

to check the first character entered to see what number was entered. Also, you need to put single quotes around the numbers (so have it case '1': and case '2'Smile because it's checking for a character, not just a number.

The error is because you need to put the while (repeat == 1) after the second bracket.

A few hints:
first of all, you really should initialize repeat to 0 when you declare it, because you don't know what it's going to start out as. Second of all, I recommend repeat being 1 all the time until you press q (have a case 'q': in your switch statement) to quit. The way it's currently, if you enter the wrong thing once, it will loop around for ever and never quit (since repeat will then be 1)
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #32
Actually, no. Notice that shape is now an int, and he's scanf'ing for %d, not %s. Just trying to head off some misinformation here...
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #33
Oops, I didn't see that he changed it from %s to %d Blush
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #34
You cant do testing for string equality in a case statement can you?

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #35
No, it must be able to be tested by a == (or it might be a primitive, I'm not sure which it is for C/C++, but I think it's the former. I know it's the latter for Java :shudder: )

Edit: if you're talking about my method of checking if it was kept as %s and shape was still a char *, since I was taking the index of one of the chars, it would have been fine, since it was just a char being compared.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #36
I'm ... pretty certain... that you can't switch on anything but an int. As in, no std::string or anything else, even if it's got an overloaded == operator. I'm at work, though, and can't test what I'm saying.
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #37
TomorrowPlusX Wrote:I'm ... pretty certain... that you can't switch on anything but an int. As in, no std::string or anything else, even if it's got an overloaded == operator. I'm at work, though, and can't test what I'm saying.

Correct. Or something that resolves to an int. From the MSDN documentation (since I don't have any C++ books handy at the moment...):

The switch statement allows selection among multiple sections of code, depending on the value of expression. The expression must be of an integral type or of a class type for which there is an unambiguous conversion to integral type

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Member
Posts: 51
Joined: 2005.07
Post: #38
yay! it works now. thanks guys. ill finish it and post it.
Quote this message in a reply
Member
Posts: 51
Joined: 2005.07
Post: #39
hmm.. there are a few bugs in it...

1. when i do a circle, it doesn't bring up the shape selectoin menu again (and maybe this happens with another, but i havent tried them all.)

2. when i do a cube, it skips some of the input commands like "Enter the length: " or something.

Code:
#include <stdio.h>

int main()
{

    int area;
    int radius;
    int length;
    int depth;
    int width;
    int height;
    int side;
    int pi = 3.14;
    int shape;
    int repeat = 0;
    
        do {
    
        printf( "\nWhat shape would you like to find the area of?\n" );
        printf( "Enter 1 for circle.\n" );
        printf( "Enter 2 for square.\n" );
        printf( "Enter 3 for cube.\n" );
        printf( "Enter 4 for cylinder.\n" );
        printf( "Enter 5 for parallelogram.\n\n" );
        scanf( "%d" , &shape );
    
        switch (shape) {
            case 1:
                printf( "\nEnter the radius of the circle:  \n" );
                scanf( "%d" , &radius );
            
                area = pi * radius * radius;
                printf( "\nThe area of the circle is %d.\n" , area );
                    break;
            case 2:
                printf( "Enter the length of one side of the square:  \n" );
                scanf( "%d" , &side );
                
                area = side * side;
                printf( "The area of the square is %d.\n" , area );
                    break;
                    
            case 3:
                printf( "\nEnter the height of the cube:  \n" );
                scanf( "%d" , &height );
                
                printf( "Enter the width of the cube:  \n" );
                scanf( "%d" , &width );
                
                printf( "Enter the depth of the cube:  \n" );
                scanf( "%d" , &depth );
                
                area = height * width * depth;
                printf( "The area of the cube is %d.\n" , area );
                    break;
                    
            case 4:
                printf( "Enter the height of the cylinder:  " );
                scanf( "%d" , &height );
                
                printf( "Enter the radius of the cylinder:  \n" );
                scanf( "%d" , &radius );
                
                area = 2 * pi * radius * radius + 2 * pi * radius * height;
                printf( "The area of the cylinder is %d." , area );
                    break;
                
            default:
                repeat = 1;
                
            }
        } while (repeat == 1);
return 0;
}
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #40
To improve it slightly you could try using less variables, you use radius for :1 and side for :2. Why not use the same variable as they are both the same type?

A good thing to learn to program loops is to make a program that will print out this kind of thing:
Code:
*
  ***
*****
*******
*****
  ***
   *
Where the user enters the number of rows.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 51
Joined: 2005.07
Post: #41
i already know how to do that, but i need to know what is wrong with my code. i have looked it over many times, but i can't find anything wrong wth it.
Quote this message in a reply
Member
Posts: 63
Joined: 2005.04
Post: #42
In my opinion the best way to learn, at first, is to follow either from a course, or from one the many great books on C. It takes faith, but you'll thank yourself later. Imo experimenting alone yields nothing. Basically, have fun, but learn while doing it
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #43
I wouldn't agree, ive spent my whole life experimenting with languages and books have been the least help for me. I think its down to the dedication of the individual.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 63
Joined: 2005.04
Post: #44
unknown- good point

My first experiences with programming were pretty experimental. yet I was somehow able to

- create a UI (IB)
- load and display 3D models
- created a tile based movement system

At the time I didn't think it was anything special, but now I look back and can't believe I did it. I suppose it's a matter of preference and choice. Do whatever feels right.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #45
yeah, I agree. Its really stange looking back, some of the acheivments seem astounding but some of the mistakes seem blindly obvious.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Post Reply