|
-
May 6th, 2002, 10:57 AM
#1
Thread Starter
Member
why is my code not working
I am new to C and i can't understand where its getting these numbers from
/* my test program */
#include <stdio.h>
main()
{
int num1, num2 , total;
printf("Hello and welcome to data link Plc \n");
printf("*********************************** \n");
printf("** Data Link ** \n");
printf("** ** \n");
printf("*********************************** \n");
printf(" \n");
printf(" \n");
printf(" Which number do you wish to add up please enter \n");
printf("the first number : " );
num1 = getc( stdin );
printf(" Which Second number do you wish to enter ? \n");
num2 = getc( stdin );
total = num1 + num2;
printf("\n");
printf("********************************************* \n");
printf("** ** \n");
printf("** %d + %d = %d ** \n", num1, num2, total);
printf("********************************************* \n");
return 0;
}
as you can see i have declare my variable ok so i want the results to look like this
23 + 2 = 25
but the user inputs any number and the program justs adds them up .
could someone explains to me why is it that its keep on giving me 50 and 60 instead.
Waheed Rafiq (ICT Technician):
Network +
-
May 6th, 2002, 01:34 PM
#2
Frenzied Member
getc() returns an integer, but that integer is a character code. It also will only retrieve one character, so any number you input with more than one digit will not be read properly.
Incidentally, getc(stdin) is the same thing as getchar(). I think getchar() is probably just a macro.
Anyway, back to your code problem....
You have some options here, depending on whether you want to make your current approach work, or whether you want to change to an easier approach.
Current approach using getc():
For each getc() you have currently you'll need to call getc() repeatedly in a loop to make sure you get all the digits of the number, append them all to a string buffer, then convert the string to an integer.
Alternative approach making life much simpler by using scanf():
Instead of using getc(), use scanf(). This is your code, modified to use scanf():
Code:
/* my test program */
#include <stdio.h>
main()
{
int num1, num2 , total;
printf("Hello and welcome to data link Plc \n");
printf("*********************************** \n");
printf("** Data Link ** \n");
printf("** ** \n");
printf("*********************************** \n");
printf(" \n");
printf(" \n");
printf(" Which number do you wish to add up please enter \n");
printf("the first number : " );
scanf( "%d", &num1 );
printf(" Which Second number do you wish to enter ? \n");
scanf( "%d", &num2 );
total = num1 + num2;
printf("\n");
printf("********************************************* \n");
printf("** ** \n");
printf("** %d + %d = %d ** \n", num1, num2, total);
printf("********************************************* \n");
return 0;
}
Harry.
"From one thing, know ten thousand things."
-
May 7th, 2002, 01:25 PM
#3
Thread Starter
Member
wow now that cool
many thanks for your help
Waheed Rafiq (ICT Technician):
Network +
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|