Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - C help needed (minor) | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
spiroth10

Octorok
Level: 9

Posts: 4/21
EXP: 2302
For next: 860

Since: 04-09-04

Since last post: 43 days
Last activity: 36 days
Posted on 05-30-04 02:15 AM Link | Quote
ok, Ive finally gone back to learning C, and im serious about it this time. I realize that c isnt really that complicated when I look at it now. But, alas, I still am not a guru, I am a newbie. to get to my problem, I was programming a calculator (just for fun) and I have the whole thing perfectly coded -- except after I ask whether the user wants to add, multiply, divide, or subtract, the program instantly ends. I have a gets(dowhat); (dowhat is a variable for the action taken add, subtract, etc) and then some if statements telling what to do for each possible answer a user might give. however, right after the program gets input, it ends. If it helps Im using a DJGPP compiler (it uses GCC and some other GNU tools...) the code is pretty short (for a C program... long if you use basic/pascal?) I split it up into two functions, int main() (duh) and math(). please help a C n00b if you can. thnx.

edit:
I cant get the code to display right, but it is layed out correctly...
the code is


#include <stdio.h>


int firstnum;
char dowhat[30];
int secondnum;
int result;

int main()
{
printf("Welcome to calculator.c! to multiply, type the word.\n");
printf("same to subtract, divide, and add, too!\n");
gets(dowhat);
math();
}
math()
if(dowhat == "multiply")
{
printf("Input the first number\n");
scanf("%s",&firstnum);
printf("Input the second number\n");
scanf("%s",&secondnum);
result=firstnum * secondnum;
printf("%s", result);
}
if(dowhat == "divide")
{
printf("Input first number (the number divided by)\n");
scanf("%s",&firstnum);
printf("Input second number (the divisor)\n");
scanf("%s",&secondnum);
result=firstnum / secondnum;
printf("%s", result);
}
if(dowhat == "subtract")
{
printf("Input the first (the number 'losing' value) number\n");
scanf("%s",&firstnum);
printf("Input the second number (the number 'taking' value)\n");
scanf("%s",&secondnum);
result=firstnum - secondnum;
printf("%s", result);
}
if(dowhat == "add")
{
printf("Input the first number\n");
scanf("%s",&firstnum);
printf("Input the second number \n");
scanf("%s",&secondnum);
result=firstnum + secondnum;
printf("%s", result);
}
}




(edited by spiroth10 on 05-29-04 05:18 PM)
(edited by spiroth10 on 05-29-04 05:19 PM)
(edited by Weasel on 05-29-04 08:59 PM)
Emptyeye
I am a real American!
Real American
Level: 67

Posts: 63/2273
EXP: 2488421
For next: 104451

Since: 05-24-04
From: I DUNNOOOOOOOO!!

Since last post: 9 hours
Last activity: 4 hours
Posted on 05-30-04 02:22 AM Link | Quote
If I'm reading your code properly, you're not actually passing anything into your math function (Unless that's what the "gets" is supposed to do). In other words, the function doesn't see a "dowhat", much less if it equals anything.

In still other words, the program is running "correctly", your code is just wrong.

I think you want something like "math(char)" in your initial function declaration, and then "math(dowhat)" in the header.

Of course, it's been a year since I've worked with this stuff myself, and I've never actually done normal C, so I could be completely wrong.
Dish

Spiny
Level: 38

Posts: 74/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 05-30-04 02:51 AM Link | Quote
You math function has to be declared before it's used. It's used in main(), so it has to be delcared BEFORE main().

put "void math();" along with your variable declarations.

your math function is also defined wrong. You have to give it a return type. just "math()" isn't going to cut it... if you don't need it to return a value, you can give it a void return type. So.. make it "void math()".

the '==' operator doesn't work the way you're thinking with strings. If you want to compare strings, use the strcmp function. strcmp will return 0 if the strings match. strcmpi will do the same thing but will ignore case.

so replace your if(dowhat == "blah") with if(!strcmp(dowhat,"blah"))

in your scanf lines... %s will get a STRING from the user. You want a decimal number... so use %d instead. Same thing with printf. %s will print a string... you're not giving it a string, you're giving it an int. So change it to %d.

I'm not sure how gets() works... so I'll leave that alone. The rest looks fine to me at first glance.
Parasyte

Bullet Bill
Level: 35

Posts: 21/514
EXP: 267348
For next: 12588

Since: 05-25-04

Since last post: 104 days
Last activity: 32 days
Posted on 05-30-04 05:40 AM Link | Quote
Just for reference, gets() works similar to scanf(), with the exaction that it will grab the entire string until it reaches a new line. As compared to scanf(), which will stop reading strings when it reaches any whitespace.
Weasel
Missionary in Peru
Level: 34

Posts: 362/454
EXP: 236444
For next: 17207

Since: 03-15-04
From: Washington

Since last post: 467 days
Last activity: 339 days
Posted on 05-30-04 06:02 AM Link | Quote
Originally posted by Disch
put "void math();" along with your variable declarations.

Note to spiroth10: what disch suggested is called a function prototype. It would be good practice to always use function prototypes...


#includes...
function prototypes

main()

functions


it's just easier to read this way generally.
spiroth10

Octorok
Level: 9

Posts: 6/21
EXP: 2302
For next: 860

Since: 04-09-04

Since last post: 43 days
Last activity: 36 days
Posted on 05-31-04 01:01 AM Link | Quote
ok... I did something totally different. I recoded it compleatly, and came up with new code that works almost perfectly... almost...(but i still think its ok, because its only my first "real" C program... my others just call you a moron or stupid stuff like that... I only started to learn C yesterday... )

though it wont give you a correct answer...
it says 2+2=100 roflol

I have no idea why this happens, i've tried redeclaring the variable that holds the value to a different type, but it still messes up...

heres my new code... if anyone has an idea on why it really screws up stuff, please, do tell me.
(again, the fromation is messed up a bit...)
___________________________________________________________


#include <stdio.h>
#include <conio.h>
#include <ctype.h>

int firstnum;
int secondnum;
int result;

main()
{
char c;
result=0;
printf("Welcome to calculator c! to multiply, type A.\n");
printf("to subtract B, divide C, and add D\n");
printf("\n");
printf("Input the first number\n");
scanf("%s",&firstnum);
printf("Input the second number\n");
scanf("%s",&secondnum);
printf("Input what function you want\n");
c=toupper(getch());
switch(c)
{
case 'A':
result=firstnum*secondnum;
break;
case 'C':
result=firstnum/secondnum;
break;
case 'B':
result=firstnum-secondnum;
break;
case 'D':
result=firstnum+secondnum-96;
break;
default:
printf("that is not a function\n");
}
printf("%i", result);
}




(edited by Weasel on 05-30-04 04:43 PM)
Weasel
Missionary in Peru
Level: 34

Posts: 365/454
EXP: 236444
For next: 17207

Since: 03-15-04
From: Washington

Since last post: 467 days
Last activity: 339 days
Posted on 05-31-04 01:50 AM Link | Quote
Almost perfectly, except completely wrong? Lol....


I have no idea how to use printf() and scanf() at all, however, I could show you how to do this stuff with cin and cout (which is c++).


As far as I could guess (guess!), your input sections are not working quite right, although I have no idea how to fix it.


Also, why are you using global variables? It's not really necessary.
neotransotaku

Baby Mario
戻れたら、
誰も気が付く
Level: 87

Posts: 646/4016
EXP: 6220548
For next: 172226

Since: 03-15-04
From: Outside of Time/Space

Since last post: 11 hours
Last activity: 1 hour
Posted on 05-31-04 01:57 AM Link | Quote
why are you reading strings as integers?

if you want to use printf/scanf, you need to know how to use the format fields correctly. this line scanf("%s",&firstnum); is saying what the user types in will be interpreted as a string and store that as an integer. which i believe is what you don't want to do. the correct code should be scanf("%d", &firstnum);.

printf - all format specifiers say how the following variables will be interpreted
scanf - the format specifier say how the input is going to be interpreted

if you want to use printf/scanf correctly, that is the idea you need to know...
Parasyte

Bullet Bill
Level: 35

Posts: 24/514
EXP: 267348
For next: 12588

Since: 05-25-04

Since last post: 104 days
Last activity: 32 days
Posted on 05-31-04 02:33 AM Link | Quote
Also,. remove that "-96" in the addition routine. The reason you were needing that for "2+2" etc is because the ascii '2' == 0x32. 0x32 + 0x32 = 0x64 = 100. As neotransotaku stated, it was reading that '2' as a string, rather than an integer.
spiroth10

Octorok
Level: 9

Posts: 7/21
EXP: 2302
For next: 860

Since: 04-09-04

Since last post: 43 days
Last activity: 36 days
Posted on 05-31-04 08:40 AM Link | Quote
thanx! The book I was reading on C did not specify that you were supposed to change file types with scanf... I guess the Dummies books aren't actually first class. hmm... ascii... I didnt know there was an ascii number system... thought it was just text... lol. as for the program almost working but being compleatly wrong, try compiling the source from the first program... it was TOTALLY off. it couldn't even handle input/output. compared to that, and the minor error I have here, this code is near perfect. BTW, weasel, the commands printf and scanf work like this, if you ever encounter them (they can be used in C++ too...)
printf("string here %s %i",string variable name, integer variable name);
scanf("%s",stringname);
change %s to the type of variable, in this case a string... thats it, other than formatting stuff. probably very similar to the C++ commands. thanks for the help!


(edited by spiroth10 on 05-30-04 11:40 PM)
Parasyte

Bullet Bill
Level: 35

Posts: 26/514
EXP: 267348
For next: 12588

Since: 05-25-04

Since last post: 104 days
Last activity: 32 days
Posted on 05-31-04 11:08 AM Link | Quote
ASCII is text. It should not surprise you that the character "2" is also text. The ASCII value for "2" is not 0x02, but 0x32. 0x32 = 50. So adding "2"+"2" will obviously get you a result of 100, opposed to 0x02+0x02=0x04; the result you were wanting. This is why you cannot add the data in text strings as if the data were valid integers.


(edited by Parasyte on 05-31-04 02:09 AM)
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - C help needed (minor) | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.025 seconds.