i'm new to programming in c++ and have been confuzzled by this for two days:

the following code was meant to create a new class ("character") and enter into it information entered by the user such as name etc.

the problem is that whenever i enter something (at one of the scanf statements) the thing crashes. i've copied the scanf statements into blank source files and they run as expected so i'm guessing the problem is something to do with the context. but without a lot of experience with c/c++ the solution has eluded me.

i know there is a great drive on this forum to force people to do their own work but i'd just like to point out that a) i'm not doing classes in programming b) i'm doing this for my own enjoyment and to further my own skills. even if you don't tell me the actual answer, some pointers on where i could find it for myself would be hugely appreciated - i don't mind work but i hate wild-goose chases through pages and pages of tutorials looking for details they just haven't given.

thanks for any help, however small.

void NewCharacter()
{
int r;
char n[30],done='n';
while (done!='y')
{
system("cls");
printf("Please select a race from the list below:\n\n1. Human\n2. Elf\n3. Half-Elf\n4. Half-Orc\n5. Dwarf\n6. Gnome\n7. Halfling\n\nNumber of chosen race: ");
scanf("%d",r);
printf("%d",r);
if (r<1) r=1;
if (r>7) r=7;
system("cls");
printf("Enter your name: ");
scanf("%s",n);
system("cls");
printf("Your chosen race: ");
PrintRaceName(r);
printf("\n\nYour name: %s\n\nIs this information correct? y/n: ",n);
scanf("%c",done);
}
}

Recommended Answers

All 8 Replies

Hi everyone,
I have written your entire code again and i think it should work

void NewCharacter()
{
int r;
char n[30],done='n',a;
//Adding a while loop here as below is not advisible as it tends to corrupt
//but it is better if you use an if statement or do loop
while (done!='y')
{
system("cls");
printf("Please select a race from the list below:\n\n1. Human\n2. Elf\n3. Half-Elf\n4. Half-Orc\n5. Dwarf\n6. Gnome\n7. Halfling\n\nNumber of chosen race: ");
scanf("%d",&r);
printf("%d",&r);
if (r<1) r=1;
if (r>7) r=7;
system("cls");
printf("Enter your name: ");
scanf("%s",n);
system("cls");
printf("Your chosen race: ");
PrintRaceName(r);
//Your last part of your program cannot be done in this way but i am
// also not sure what you want so i just added this to help you
printf("\n\nYour name: %s\n\nIs this information correct? y/n: ",n);
scanf("%c",&a);
//This is the right way to do it
if(a=='a')
{
//do something
}
else
{
//do something again
}
}

Yours sincerely

Richard West

Greetings.
Umm, I don't quite get you.
What would be the expected output & the relevant input?
Could you please attach also the content of functino PrintRaceName(int) ?
Thanks.

scanf("%d",&r);

ahhh...... thats the problem. that was a silly mistake to make. a very silly mistake. no matter how many times i looked at it i couldn't see that. lol.

Could you please attach also the content of functino PrintRaceName(int) ?

its a bit messy actually...

void PrintRaceName(int rn)
{
char *ptr=racenames[rn-1];
printf("%s",ptr);
}

racenames[] is an array of pointers to strings - each string containing the name of a race.
there's probably a much better way of doing it but this way works for now :D - "(1)Get it to work, (2)Get it to work correctly, and (3)Get it to work efficiently."

thanks for all your help once again.

So there is always cin>>.:D

thanks for the advice. can't believe i didn't spot that it should have been &r not just r... maybe i gotta go do those tutorials again.
btw - the array that stored pointers to the strings - was that the right way to go about it? or is there a better/neater/simpler way of doing it?

can't believe i didn't spot that it should have been &r not just r... maybe i gotta go do those tutorials again.

I have stared at typos for ages and not noticed them. The best learning you will encounter with programming is making mistakes. :)

scanf("%d",r);

The above statement is wrong logically. The correct statement would be
scanf("%d",&r);

You would be storing the value entered by the user at the address of r. So you have to use &r.

So modify the other scanf statements. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.