Hey, this is my 2nd day of programming C++ and i have coded a program, it seems to stop working after one of the questions i have set, I tried many diffirent code scenarios but they all result the same, im just testing it for a game engine im planning on coding here it is, hope you can help
#include<stdio.h>
int main (void){
char name[10] = {'\0'};
int userage;
char gender;
int answer;
printf("What Is Your First Name?");
scanf("%s", &name);
getchar();
printf("How Old Are You?");
scanf("%d", &userage);
getchar();
printf("What Is Your Gender (Male/Female)?");
scanf("%s", &gender);
getchar();
if(userage < 12){
printf("Sorry %s, But You Are Too Young To Play", name);
} else {
printf("Thank You For Joining us %s", name);
getchar();
}{
printf("Please Confirm That You Are %d, Your Name Is %s and you are %s (1=Yes/0=No)", userage, name, gender);
scanf("%d", &answer);
getchar();
if(answer < 1){
printf("Please Proceed To Re-enter your information");
} else {
printf("Thank You %s, Please Proceed To Create Your Character", name);
getchar();
return 0;
}
}
}
i tried your solution, sorry but it didnt work :/ and as i said, i tried every scenario, therefore ending andstarting the code with french brackets thanks anyways
you haven't really given us enough information, what goes wrong, where it occurs in the code. Put a breakpoint at the start of your program and then step through it line by line with f10, then you'll see where the problem is. If you're still having trouble then come back and post what you know about the problem.
Thanks for the help, but as i said, im 3 days new to coding and i dont really know ive tried everything i know about, could you test the code in Dev C++? because for me, whatever i do it just says it stopped working after the first 'else' code nothing appears then it says its looking for a solution to the problem
hello my friend,y took a look at you're source code to see what can be the problem,and y got it to work.there where some errors and that is why you're program crashed.as an advice y will give you some "tips" so you're code can be read and understand better,because you're brackets are a bit confusing in the way you arranged them.take a look at the code y posted.(y a a beginner to so if someone has an advice,or to correct me is some mistake let me now.we all learn.)
int main ()
{
char name[10];
int userage;
char gender[10];
int answer;
printf("What Is Your First Name? ");
scanf("%s",&name);
printf("How Old Are You? ");
scanf("%d",&userage);
printf("What Is Your Gender (Male/Female)? ");
scanf("%s",&gender);
if (userage < 12)
printf("Sorry %s, But You Are Too Young To Play\n",name);
else{
printf("Thank You For Joining us %s\n",name);
printf("Please Confirm That You Are %d, Your Name Is %s and you are %s (1=Yes/0=No) ",userage,name,gender);
scanf("%d",&answer);
// put each if with it's corresponding else one under the other so you can und. better
if (answer < 1)
printf("Please Proceed To Re-enter your information\n"); //one instruction,no {}
else
printf("Thank You %s, Please Proceed To Create Your Character\n",name);
}
system("PAUSE");
return 0;
}
the mistake was where you tried to read the gender.if you use %s,then it means u have a string.and a string is composed of some characters and '\0'..so if you type 'm' (for male),you will have 'm' and '\0'.and char type has room just for one character,not 2..something like that(i am not that good :)).and don't put so many getchar(),where you don't need to..read more about the getchar() function in the reference,there it is explained better that y can..well y hope y helped you in some way,and keep on the good job :)
thank you so much, not only was your code alot shorter than mine, it actualy worked :p i will take this and use it next time i need to do something like this, by the way, thhe tutorial i was following said to put the getchar() and other stuff, i will remember not to next time, again, thanks