GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Oct-2006, 20:47
Targsmom Targsmom is offline
New Member
 
Join Date: Oct 2006
Posts: 2
Targsmom is on a distinguished road

Math problem: Total Noob


Hello. I'm taking a C course right now and I'm stumped.. I can't figure out why my code isn't working.

Here is my homework assignment: (no I'm not looking for anyone to do this for me.. and please be kind. It's my first C course)


1) present a menu to the user from which there will be 6 choices
A -> Add two numbers
S -> Subtract two numbers
M -> Multiply two numbers
D -> Divide two numbers
O -> Modulus two numbers
E -> Exit
2) If the user enters a A, S, M, D or a O the program will prompt the user for two numbers and perform the needed math on them and output the number.
3) If the user enters an E then the program will thank the user for trying to use the program then exit nicely.
4) The program must use If ?else statements.
5) The program should use floating point numbers for all numbers entered. But you must cast them to an integer for the modulus.

I'm stumped on the casting, but I'll worry about that later. This is my code snippet:
CPP / C++ / C Code:
#include <stdio.h>

int main()
{
	char option;
	float num1;
	float num2;
	float result; /*Defining variables*/


            printf("For Addition, please enter A \n");
            printf("For Subtraction, please enter S \n");
            printf("For Mutiplication, please enter M \n");
            printf("For Division, please enter D \n");
            printf("To find the remainder, please enter O\n");
            printf(To exit the program, please enter E\n"); /*This will print a menu in a format that I hope is readable*/

	scanf("%c", &option);

	if (option == 'A')
	{
		printf("Please enter two numbers you wish to add.\n");
		scanf("%f %f", &num1, &num2);
		result = num1 + num2;
		printf("The total is %f, result");
	}


	else if (option == 'S')
	{
		printf("Please enter two numbers, with the second to be subtracted from the first.\n");
		scanf("%f %f, &num1, &num2");
		result = num1 - num2;
		printf("The differenc is %f, result");
	}


	
	return (0);
}


When I compile and run this every answer I get is:
The total is 0.000000, &result

Obviously I have something boneheaded in there.. and I've got 4 books and Google and no idea what I missed.

Can someone help me?

Thank you!

Jennifer
  #2  
Old 19-Oct-2006, 23:41
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Math problem: Total Noob


Jennifer, THANK YOU for reading the Guidelines before posting! You are a pearl in the .... better stop there

First thing you should always do when you run up against a problem like this is print the values entered to see if they are correct. You may find they aren't. scanf() is a notoriously ill-behaved function, and I think you might be getting caught in the dirty-input-stream problem. See this.

Also, since all your operations work on 2 numbers, you could read the numbers before your IFs and have only one place for reading. But if you plan on accepting 1 or 3 numbers at some point, this may not be a good thing to do.

[edit]I just noticed your printf() for the result is wrong. Look carefully.[/edit]
__________________

Age is unimportant -- except in cheese
  #3  
Old 19-Oct-2006, 23:47
niek_e niek_e is offline
Junior Member
 
Join Date: Sep 2006
Posts: 39
niek_e is on a distinguished road

Re: Math problem: Total Noob


And now to solve your problem:

Code:
printf("The differenc is %f, result");

should be:

Code:
printf("The differenc is %f", result);

(I replaced the quote)

Just a tiny syntax error!

gr Niek
  #4  
Old 20-Oct-2006, 05:05
Targsmom Targsmom is offline
New Member
 
Join Date: Oct 2006
Posts: 2
Targsmom is on a distinguished road

Re: Math problem: Total Noob


Quote:
Jennifer, THANK YOU for reading the Guidelines before posting!

Waltp, Nothing annoys me more than people who don't. I also think I'm going to tape RTFM on my wall at my desk at work.. But thank you. And thank you for the link. I'll read that later today.. Interesting idea to read all the numbers first. Hrrrmmmm.... I also appreciate the idea that we may need to change that later.. I shall contemplate. Thanks!


Quote:
Just a tiny syntax error!

Thanks niek_e! That would be the "boneheaded" piece I couldn't find! Quotes always goof me up. You should have seen me trying to teach myself html!
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 10:09
Problem with DocumentRoot with apache 2.0 on Red Hat Linux(im a noob) vjender Apache Web Server Forum 0 21-Aug-2005 05:05
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 07:03
Hash table problem. penance C Programming Language 1 24-Jul-2005 18:50
Small problem Krc784 C++ Forum 1 05-Nov-2004 08:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:31.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.