Like Tree2Likes
  • 2 Post By quzah

Need help asap!!! Please!!!

This is a discussion on Need help asap!!! Please!!! within the C Programming forums, part of the General Programming Boards category; I am writing a calculator code.. I am having issues with input validation. I can't get in touch with my ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    6

    Exclamation Need help asap!!! Please!!!

    I am writing a calculator code..
    I am having issues with input validation.
    I can't get in touch with my instructor as I am taking an online course.
    I am in need of assistance....

    I need to make it so my calculator forces the user to input integers for the menu options as well as for the calculations...
    If they put a letter the program loops.
    I need it to send an error to the user.
    I have tried isdigit...but it isn't working.
    i am new.
    my code is below:

    Code:
    /*Midterm Project: Simple Calculator*/
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    
    {
    	int option,n1,n2,ans;
    
    	
    
    	
    
    	printf("\n\n\n\n\n                Welcome to (name) Handy Calculator!     \n\n\n\n\n\a");
    	
    	printf("                       Press any key to continue!\n");
    	getch();
    	do{
    	system("cls");
    	printf("\n\nPlease select an option from the list below.\n\n");
    	printf(" 1. Addition \n");
    	printf(" 2. Subtraction \n");
    	printf(" 3. Multiplication \n");
    	printf(" 4. Division \n");
    	printf(" 5. Exit \n\n");  
    	scanf("%d",&option);
    	system("cls");
    
    
    
    	if(option==1)
    	{ 
    		printf("You have chosen Addition! Please select two numbers to add.\n");
    		printf("Enter first number...\n");
    		scanf("%d",&n1);
    		printf("Enter second number...\n");
    		scanf("%d",&n2);
    		ans=n1+n2;
    		printf("The answer is %d!\n\n",ans);
    		system("pause");
    		
    
    	}
    	else if(option==2)
    	{
    		printf("You have chosen Subtraction! Please select two numbers to subtract.\n");
    		printf("Enter first number...\n");
    		scanf("%d",&n1);
    		printf("Enter second number...\n");
    		scanf("%d",&n2);
    		ans=n1-n2;
    		printf("The answer is %d!\n",ans);
    		system("pause");
    	}
    	else if(option==3)
    	{
    		printf("You have chosen Multiplication! Please select two numbers to multiply.\n");
    		printf("Enter first number...\n");
    		scanf("%d",&n1);
    		printf("Enter second number...\n");
    		scanf("%d",&n2);
    		ans=n1*n2;
    		printf("The answer is %d!\n",ans);
    		system("pause");
    
    	}
    	else if(option==4)
    	{
    		printf("You have chosen Division! Please select two numbers to divide.\n");
    		printf("Enter first number...\n");
    		scanf("%d",&n1);
    		printf("Enter second number...\n");
    		scanf("%d",&n2);
    		ans=n1/n2;
    		printf("The answer is %f!\n",ans);
    		system("pause");
    
    	}
    	else if(option==5)
    	{
    		exit();
    	}
    	
    	}while(option!=5);
    		
    
    
    
    	return 0;
    	
    	
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Take a look at this thread: Making a switch error proof

    We went over reading input here fairly well.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    i'm sure you did...
    I need help and direction.
    please don't respond unless you will actually help me.
    i don't know any programmers that i could even ask for assistance....

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So you are too lazy to click and read a link on the same forum asking the same type of question you are asking? Yeah... have fun helping yourself.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    take a hint and get lost!!!...i have been reading..i'm new and a little confused...

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    oh and... i did try the switch..if i knew what i was doing wrong..i would fix it and not ask for help...

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by calibean View Post
    I need to make it so my calculator forces the user to input integers for the menu options as well as for the calculations...
    If they put a letter the program loops.
    I need it to send an error to the user....
    First off quzah did point you in the right direction. Secondly your program is looping if the user inputs anything besides 1-5 because you tell it to:
    Code:
    ....
    }while(option!=5);
    Note in the discussion quzah posted it discussed using the return value from scanf(). Take a look at this simple example:
    Code:
    int main(void){
    
    	int num, read;
    
    	printf("Enter a number:");
    	read = scanf("%d", &num);
    	printf("%d", read);
    
    	getchar();
    	getchar();
    	getchar();
    	return (0);
    }
    Guess what gets printed if the user enters a character?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    Thank you. I don't mean to be rude. I'm just frustrated
    I did look at what he pointed me to... but alot of this stuff is still foreign to me.

    I need someone who will patiently help me and not be cocky and rude about it.

    I'm sorry Quzah.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by calibean View Post
    take a hint and get lost!!!...i have been reading..i'm new and a little confused...
    Wow... you gotta work on that temper.

    We are NOT going to hand you finished code... you won't learn a thing that way.

    The answer to your problem lies in checking the return value of scanf() ... it returns the number of successful conversions (inputs). What do you think it returns if the user enters an unconvertable input?

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Ok, so right here in your code is where you are taking user input and it could be where you could do the initial error check for your input:
    Code:
    scanf("%d",&option);
    system("cls");
    So, IF you wanted to check your input and then PRINT and error message how would you think you would go about it. I am sure that you ran the above sample code I posted and by now realize that scanf returns the number of successful conversions it performs.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    Thank you Andrew.
    I apparently need a mentor...and after my midterm...a vacation. haha

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    No worries. You should have what information you need now to get the program working. The continue keyword would be something to take a look at as well.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone Please Help! ASAP!!
    By CB44 in forum C Programming
    Replies: 1
    Last Post: 01-16-2011, 04:41 PM
  2. Please help asap!!! :(
    By unbrknchane in forum C Programming
    Replies: 3
    Last Post: 02-25-2010, 08:53 AM
  3. need help ASAP ,
    By Haytham in forum C Programming
    Replies: 3
    Last Post: 05-14-2007, 10:21 AM
  4. Need Help ASAP....
    By Maxwell in forum C++ Programming
    Replies: 16
    Last Post: 09-14-2005, 06:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21