User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » Need some help programming in C Page [1]  
Wraith
All American
27257 Posts
user info
edit post

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;
}


The problem I'm having when I run this is that it gets to the "Please enter your first number" part and I can enter the value for x fine. It then displays the "Please enter your second number" but seems to completely bypass the "y = getchar();" section of code and go straight on to calculations. Obviously it calculates the incorrect answers as a value is never assigned to y. For example if I were doing it in fortran, that section would just be

Write (*,*) "Enter your first number"
Read (*,*) x
Write (*,*) "Enter your second number"
Read (*,*) y

Any help is appreciated.

[Edited on June 27, 2007 at 10:41 AM. Reason : code]

6/27/2007 10:16:01 AM

crazyOx
New Recruit
12 Posts
user info
edit post

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

factotum
New Recruit
38 Posts
user info
edit post

The function "getchar" reads a single ASCII character.

You are trying to read a series of digits such as 106 and have that
converted 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

Wraith
All American
27257 Posts
user info
edit post

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

crazyOx
New Recruit
12 Posts
user info
edit post

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

factotum
New Recruit
38 Posts
user info
edit post

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 is
a 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 functions
differ 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

crazyOx
New Recruit
12 Posts
user info
edit post

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

Wraith
All American
27257 Posts
user info
edit post

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

f15smtd
Starting Lineup
78 Posts
user info
edit post

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 people
strcpy(people[0].fullname,"John Doe");
people[0].age=35;


Why is it that when I do the last 2 lines, the elements in the first struct are not getting any values (zero's and blanks when i printf them)? I guess it has something to do with the way I created the arrays?

7/3/2007 2:08:31 PM

factotum
New Recruit
38 Posts
user info
edit post

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 people
person people[10]; // creates 10 people


// clear the structure
for (i=0;i<10;i++)
{
strcpy(people[i].fullname,"Empty");
people[i].age=0;
}


// set 1 person
strcpy(people[0].fullname,"John Doe"); // sets name
people[0].age=35; // sets age


// print the structure
for (i=0;i<10;i++)
printf("%s %d \n", people[i].fullname, people[i].age);


}

7/4/2007 1:04:35 PM

 Message Boards » Study Hall » Need some help programming in C Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.39 - our disclaimer.