Haha this sucks. I'm a fortran wizard but I just started learning C yesterday at work and feel like a complete programming n00b. I've been teaching myself through some tutorials and have gotten the gist of it but I can't for the life of me figure out what the problem is with this basic program I am trying to write. I'm just trying to get the hang of inputting and outputting variables.
#include <stdio.h>main (){ int x, y; int sum, diff,prod,quot; printf ("Please enter your first number:\n "); x = getchar(); printf ("Please enter your second number:\n "); y = getchar(); sum = x+y ; diff = x-y; prod = x*y; quot = x/y; printf ("\n The sum is: %d", sum); printf ("\n The difference is: %d", diff); printf ("\n The product is: %d", prod); printf ("\n The quotient is: %d", quot); return 0;}
6/27/2007 10:16:01 AM
Hi,Two things to help you get started. Your main method should return an int type. The second is use the scanf method for your input.
6/27/2007 10:31:08 AM
The function "getchar" reads a single ASCII character.You are trying to read a series of digits such as 106 and have thatconverted to an integer (hex 6A).That requires SCANF in C.In C++ there is alternate way ... x << cin
6/27/2007 10:33:54 AM
Okay I looked up scanf and got it working, thanks guys.So for future reference, I should only use getchar when I have only a single character to enter? Wouldn't it be easier to just use scanf all the time?
6/27/2007 10:52:07 AM
If you are working with character I/O, then use the getchar method, otherwise i would use scanf method. Could someone clarify more on this. I'm a little rusty with my C.
6/27/2007 11:05:36 AM
SCANF is significantly more flexible and complicated and thus more prone to errors.I agree with crazyOx, that if you only need a single ASCII character, then getchar isa better choice.If you have a C reference, you will find many different and specific I/O functions.For example, "gets" and "puts" will read whole lines of text. Many of these functionsdiffer in how they handle, store and convert new line characters. So you can often find a specific function that matches your need,thus reducing the code you have to write.
6/27/2007 11:21:21 AM
If the tutorial you were following asked you to use the getchar method to input the numbers, then i suggest you use another tutorial. That would be a really bad tutorial. If you need more pointers to some tutorials, just let us know.
6/27/2007 12:27:20 PM
Yeah I just found a better tutorial actually, I browsed ahead a little on the other one and couldn't even find anything on the scanf method.
6/27/2007 1:39:30 PM
I've got a question about an array of structs that someone might be able to answer (i'm doing this in C, not C++, if that makes any difference).Say we have:
typedef struct{ char fullname[50]; int age;} person;person people[10]; // creates 10 peoplestrcpy(people[0].fullname,"John Doe");people[0].age=35;
7/3/2007 2:08:31 PM
I think your code is okay.I just converted it into a whole program and it runs correctly.You may have been missing the include for string.h in your program.void main(){#include <stdio.h>#include <string.h>int i;typedef struct { char fullname[20]; int age; }person;// creates 10 peopleperson people[10]; // creates 10 people// clear the structurefor (i=0;i<10;i++) { strcpy(people[i].fullname,"Empty"); people[i].age=0; }// set 1 personstrcpy(people[0].fullname,"John Doe"); // sets namepeople[0].age=35; // sets age// print the structurefor (i=0;i<10;i++) printf("%s %d \n", people[i].fullname, people[i].age);}
7/4/2007 1:04:35 PM