Thread: Help need in program

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    21

    Help need in program

    I am trying to create a program, in c, that will do that following:

    It displays "Enter the operation of your choice: a. add s. subtract m. multiply d. divide q. quit"

    It asks for the input from the user and then asks for 2 numbers and then executes the operation chosen by the user.

    I have no clue to even how to start this program. The furthest I have gotten is the printf command. Please help me with this program. Any help will be appreciated. Thank you in advance.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Post the code you have tried thus far.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    We won't do your homework for you. You show your work by posting a program (even if incomplete), and we'll help answer any specific questions or problems you have with it.

    We're not a homework forum.

    Best advice would be to study your class material, and also work through one of the many C tutorial sites that Google will list for you when you search for C tutorials.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    26
    Well fiendslyr this is a pretty stright forward question.

    >> After you are done either your printf probably you could ask the user for his input - Choice of operation (Done thru scanf function) -- And then those two numbers and store them in two variables!

    >>
    Then use a switch case statement to perform the respective operations based pn your input variables. I assume you get this!

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    	
    	printf("Enter the operation of your choice:\n"
    				"a. add 		s. substraction\n"
    				"m. multiply 	d. divide\n"
    				"q.  quit"); /*displays the options for the user*/
    
    	scanf("")
    return EXIT_SUCCESS;
    
    }
    This is what i have so far. I know to use the scanf command to ask for an input. now, if the input is a characater, what is the code for that (for ex. i know an integer is %d). also,
    i guess my variable would be something like "char choice" how would go about storing the input into that variable or would i use an if function or another type of loop?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    now, if the input is a characater, what is the code for that (for ex. i know an integer is %d)
    use %c for reading characters... if you know how to read in integers already, just change the %d to %c, and give it a "char" variable to store it in, rather than an "int" variable, of course. to read it in you use "scanf" as you have, not an "if" or a type of loop. give it a shot and let us know!

    edit: later, for reading numbers maybe the FAQ will help you.
    Last edited by nadroj; 09-13-2009 at 11:36 AM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You know about EXIT_SUCCESS, but don't know how to get a char input using scanf()?

    That's a first!

    perhaps:
    Code:
    char choice;
    
    //and later:
    scanf(" %c", &choice);

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> i guess my variable would be something like "char choice" how would go about storing the input into that variable or would i use an if function or another type of loop?

    Yes, you could declare the input as char, and pass that to the scanf function to retrieve the user's selection.

    >> I know to use the scanf command to ask for an input. now, if the input is a characater, what is the code for that (for ex. i know an integer is %d).

    Before using any API, the first order of business is to get a hold of some good documentation. It would probably be a good idea to find a downloadable reference, as well.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    ok, at this point, i tried to compile it and it gave a build error in eclipse. "file minicalc.exe: Permission denied" why would it give me that error? i tried deleting it and creating a new one and still gave me that error.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    assuming you do have permissions to wherever the output file (.exe) is to be created, my only guess is that you are already running this file in some other window or in the background, check your OS' task manager and make sure its not already running and if so, kill it and try to build again.

    alternatively, you can post your updated code and we can verify that it should work and work the way you intend.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    thanks. yea, i figured out that it was still running.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main(void)
    {
    	//declared variables for choices, result, and inputed numbers
    	char operation;
    	double operand1;
    	double operand2;
    	double result;
    
    	printf("Enter the operation of your choice:\n"
    				"a. add 		s. substraction\n"
    				"m. multiply 	d. divide\n"
    				"q.  quit"); /*displays the options for the user*/
    
    
    	scanf("%c", &operation);
    
    return EXIT_SUCCESS;
    }
    right now, the only problem im having when i run it is the fact that it doesn't display the printf first. there is a blank line and i press enter and then it will display the printf and then it just terminates. Why is that?
    Last edited by fiendslyr; 09-13-2009 at 01:17 PM. Reason: change of code

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    you must not be running the code you have posted. make sure to rebuild the project to create the new .exe and run that one. also make sure your code matches what you pasted here.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by fiendslyr View Post
    thanks. yea, i figured out that it was still running.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main(void)
    {
    	//declared variables for choices, result, and inputed numbers
    	char operation;
    	double operand1;
    	double operand2;
    	double result;
    
    	printf("Enter the operation of your choice:\n"
    				"a. add 		s. substraction\n"
    				"m. multiply 	d. divide\n"
    				"q.  quit"); /*displays the options for the user*/
    
    
    	scanf("%c", &operation);
    
    return EXIT_SUCCESS;
    }
    right now, the only problem im having when i run it is the fact that it doesn't display the printf first. there is a blank line and i press enter and then it will display the printf and then it just terminates. Why is that?
    Output is almost always "line buffered" which means it only gets printed when the line is finished . Since you end your printf with an incomplete line, it won't get printed. And of course it just terminates since you don't actually do anything with the code after the scanf except stop.

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    All the code is correct and I am rebuilding each time and there are no other running instances. It seems that for me to get the program to actually start, I have to press enter.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by fiendslyr View Post
    All the code is correct and I am rebuilding each time and there are no other running instances. It seems that for me to get the program to actually start, I have to press enter.
    So did you fix your code yet by putting "\n" behind "quit"?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM