Question:
c questions that i have?
anonymous
2010-12-06 13:46:24 UTC
Here's the source code of a small calculator that i've made.

http://pastie.org/1353584

Now the questions:

Im using the goto function to always get me back to the beggining when the operation is complete or the user inputs something that he isn't supposed to input.
Works great with numbers but if the user inputs a letter, a infinite loop is started:
Why is this happening?
Is there a better way to do it?
The program only does operations with two integers:
What if i want to let the user chose the number of numbers:
How do i tell the compiler to create variables for the numbers the user wants to add or whaterver?
What if the user wants to use floats?
Can i convert a int to a float?
If yes give me a link explaining how to do it, or just explain it yourself.
Other questions not related to the program:

Why do i have to void main? I tried not voiding it and the results are the same.

I should i use return 0?
It's the same was not using it.
So what does return do?

Thanks in advance.
Four answers:
cja
2010-12-06 14:09:20 UTC
I can't access pastie.org from where I am now, but I think I can answer your questions.



> I'm using the goto function to always get me back to the beginning when

> the operation is complete or the user inputs something that he isn't

> supposed to input.

>

Conventional wisdom says that using goto is bad programming practice. In the hands of experts, a goto may not be bad. In general, it's a good idea not to use it. For this program, I'm sure you don't need a goto. A normal 'for', 'while', or 'do-while' loop will work.



> Works great with numbers but if the user inputs a letter, a infinite loop is started:

> Why is this happening?

>

You're probably using scanf.



> Is there a better way to do it?

> The program only does operations with two integers:

>

Yes. Use fgets and sscanf. E.g., to get two floats from the user, use a function like this:



void getFloat2(const char *p, float *a, float *b) {

    bool inputOk;



    printf("%s : ",p);

    for (inputOk = false; inputOk == false; ) {

        fgets(line,MAX_LINE_LEN,stdin);

        inputOk = (sscanf(line,"%f ,%f%s",a,b,junk) == 2);

        if (inputOk == false) {

            printf("invalid input, try again: ");

        }

    }

}



If called this way:



    getFloat2("Enter x,y",&coords[i].x,&coords[i].y);



The user can do this:



Enter x,y : 1.1,2.2

Enter x,y : 3 , 4

Enter x,y : 5.5, 6.6



I've defined 'line' and 'junk' to be arrays of char, with the size MAX_LINE_LEN. I've also declared bool:



typedef enum { false = 0, true } bool;



> What if i want to let the user chose the number of numbers:

> How do i tell the compiler to create variables for the numbers the user

> wants to add or whatever?

>

You can assume a maximum number of inputs, and declare an array that size. Or, have the user tell you how many numbers he wants to enter, and dynamically allocate an array, using malloc or calloc, to be filled with user input.



> What if the user wants to use floats?

> Can i convert a int to a float?

>

Yes. As you see above, you can scan for float, and if an int is entered it's ok.



> Why do i have to void main? I tried not voiding it and the results are the same.

>

You don't have to, and I don't think you should, declare main to be a function returning void. The standard declaration of main is this:



    int main(int argc, char *argv[]);



> I should i use return 0?

>

Zero is a fine value to return.



> It's the same was not using it.

> So what does return do?

>

Returns a value to the caller. In the case of main, and especially with a console program you create, this return status is probably not used.
lansingstudent09101
2010-12-06 14:22:44 UTC
scanf is skipped because it still HAS a character in it and is waiting for that to be read, it does not need to ask the user for a next character. So, to consume the character, you'd do something like call "flushall()" to empty it out.





Yes there is a better way. A while loop, gotos are dangerous, and frowned upon in general. While loops with break and/or continue would be better.



A variable number of numbers will probably require an entire program rewrite. The best way would be to use lex and/or yacc to truly build a grammar to handle it. The second best way would be to create some data structure with pointers and use a loop to add them. Something like



struct numberstack{

int thenumber

struct numberstack * next

}



I'm not sure what you mean "convert int to float". If you use a variable that has the int type and add it to a float, it is first transformed into a float, and then added implicitly. By that I mean you can add 7.0 and 8 and the result is 15.0 (a float). If you want to force an int to be used as a float in a situation where it wouldn't normally be (integer division maybe). add:



(int)



in front of it.



In C, putting void in main saves you memory. If you don't give main a void arguments list, it allocates for a variable list of arguments, which wastes memory.



main returns to the operating system. In many operating systems for instance you can use "shell scripting" and get that value back explicitly. As a convention, especially in Linux and Unix, return values of 0 are normal, and other values indicate that the operation did bad things and needs to be either handled or the script aborted. (some applications return things like "number of bits written" though). This is not so much used by Windows, but duggers/analyzers, system watchers, may check the return values.
tfloto
2010-12-06 14:03:58 UTC
I can't get to your site, it's blocked. But here are some thoughts:

1. Always take input as a string, test to see if its a legal number then convert it. You can ignore other input or warn the user of bad input.

2. you can create an array or list to hold the numbers the user wants to operate on.

3. you can convert to and from int an floats it's just a cast. the compiler will warn you about truncating a value but it's ok. If you don't know about casting: the below examples are explicit casts.

int z = 32;

float f = 96.0;

z = (int) f;

f = (float) z.

see the link below for more info on casting



most compilers let you declare main in one of several ways:

int main();

int main(int argc, char ** argv);

void main();

void main(int argc, char ** argv);



the compiler adds code to make it look right to the operating systme.
jplatt39
2010-12-06 14:00:30 UTC
You convert to a float by say:

float y;

int x;

...

y=(float) x;



The best way to handle input is with a switch statement or a state machine. Always. I will be honest and say I haven't looked at your code but EVEN if your default input is numbers, you check the input, if it is a numeric character call a function which multiplies the default integer by ten and adds the new integer, unless the user has already entered a period (which is why a state machine is better for some things, if the value is x the default for a correct entry is to multiply the default number by ten and add the integer, the default for y is check how many places after the decimal point the default number has and divide the new entry by ten that many times and one more time before adding that to the number, and so on. When the user enters a value the system doesn't understand, the default option becomes the error message. Look up switch statements and state machines.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...