• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

question about c programing

corajr

n00b
Joined
Nov 12, 2004
Messages
30
I been reading some c programing tutorials in the web but so far i have nto found any good tutorial that is for a begginer.

I am having trouble with the following program im trying to put together:

/* This is a program i created by my self*/
#include <stdio.h>
main()
{
int person_fname;
int person_age;
/*long int my_age;*/


printf("This program will tell you if you are older or younger than the programer\n");
printf("Please enter your first name and press [Return]\n");
scanf("%c", &person_fname);
getchar();
printf("Now please enter your Age and press [Return]\n");
scanf("%c", &person_age);
getchar();
printf("Very well now i know that your name is %c and that you are %c old\n", person_fname, person_age);
getchar();

}


Here is the problem when I compile and run the program, if I type more then one character the program crashes. am I putting the wrong data type for my variables? This is my first time trying to learn how to program I have read everything I been able to put my hands on but none of the tutorials out there are very noob friendly, to some degree they expect you to know some coding ect ect.

Thanks in advance for any help.
 
It's been about 9 years since I programmed in C++ but try changing it to this:

/* This is a program i created by my self*/
#include <stdio.h>
main()
{
char person_fname[25];
int person_age;
/*long int my_age;*/


printf("This program will tell you if you are older or younger than the programer\n");
printf("Please enter your first name and press [Return]\n");
scanf("%s", person_fname);
printf("Now please enter your Age and press [Return]\n");
scanf("%s", person_age);
printf("Very well now i know that your name is %s and that you are %s old\n", person_fname, person_age);

}
 
Use %i, not %s, when you want to read or print an int.
The most commonly useful ones are %s for string, %i for integer (signed) and %c for a single character.

If you are on a linux system, check man scanf.
 
HHunt said:
Use %i, not %s, when you want to read or print an int.
The most commonly useful ones are %s for string, %i for integer (signed) and %c for a single character.

If you are on a linux system, check man scanf.


cool that worked like a charm thanks a lot, to bad the websites and tutorials that i have read do not talk about everything you need to know in depth=(
 
corajr said:
cool that worked like a charm thanks a lot, to bad the websites and tutorials that i have read do not talk about everything you need to know in depth=(

The absolute best method is to use man pages. If you don't have man-pages on your system you can use google: "man page" string.h for example.
 
well finally here is my first c program my pride and joy :D

/* This is a program i created by my self*/
#include <stdio.h>
main()
{
char person_fname[25];
int person_age;
int my_age;


printf("This program will tell you if you are older or younger than the programer.\n");
printf("Please enter your first name and press [Return].\n");
scanf("%s", &person_fname);
getchar();
printf("Now please enter your Age and press [Return].\n");
scanf("%i", &person_age);
getchar();
printf("Very well now I know that your name is %s and that you are %i years old.\n", person_fname, person_age);
getchar();
if (person_age > 26){
printf("You are older than the programer who wrote this application.\n");
}
else if (person_age < 26){
printf ("You are younger than the programer who wrote this application.\n");
}
else if (person_age == 26){
printf ("You are the same age as the programer who wrote this.\n");
}
getchar();
}
 
doh said:
The absolute best method is to use man pages. If you don't have man-pages on your system you can use google: "man page" string.h for example.

cool will start doing that from now on i am buying a laptop soon in which im going to install ubuntu and use Anjuta IDE i love that program
 
corajr said:
well finally here is my first c program my pride and joy :D
Code:
/* This is a program i created by my self*/
#include <stdio.h>
main()
{
 	  char person_fname[25];

When you realise why that is dangerous you will be one step closer to a C guru. ;)
 
Back
Top