•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 427,219 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,256 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1287 | Replies: 10
![]() |
•
•
Join Date: Dec 2007
Posts: 9
Reputation:
Rep Power: 0
Solved Threads: 0
Ale
Hallan people..
I have something like this:
When i choose for the first time it's okay.....after i'm done with my first action it prints the MENU (it's good) and autimatically prints the default option(error:...bla bla) and the menu again.....Means it's skips the 'gets'.....WHY? Any ideas? i tried the 'scanf', 'getchar', everything....what is the right way to check input to be valid? AND ONCE AND FOR ALL! CAN SOMEONE TELL ME WHAT'S WRONG WITH THE 'GETS' FUNC? IT TOTALLY DOES IT'S OWN THING AND I DON'T KNOW WHAT IT IS.
Hallan people..
I have something like this:
void main(){
int flag=1,exit=1;
cla_N *head=NULL;
char class_name[NAME_LENGTH],choice[1];
double diversity;
while (exit)
{
printf("Please choose one of the options below:\n"
"1- Create/update class. \n"
"2- Delete class. \n"
"3- Insert new species. \n"
"4- Delete species. \n"
"5- Update species. \n"
"6- Print species. \n"
"7- Print class. \n"
"8- Exit. \n");
//scanf("%d", &flag);
gets(choice);
//choice=getchar();
//scanf("%c",&choice[0]);
switch (choice[0])
{
case '1':
printf("Enter class name:\n");
//gets(class_name);
gets(class_name);
printf("Enter diversity:\n");
scanf("%lf", &diversity);
head=func1(head,class_name,diversity);
break;
case '7':
printf("Enter class name:\n");
//gets(class_name);
gets(class_name);
func7(head,class_name);
break;
case '8':
exit=0;
break;
default:
printf("Error: input incorrect! Please try again.\n");
break;
}
}
} > void main()
This is wrong, main returns int.
http://c-faq.com/ansi/voidmain.html
http://faq.cprogramming.com/cgi-bin/...&id=1043284376
> gets()
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
And while I'm on a roll, try this as well
http://www.catb.org/~esr/faqs/smart-...html#writewell
The real problem is you're mixing scanf() with gets() (ugh!!!) and getchar()
If you mix and match, as you're doing, then you'll come unstuck. The reason being they have different ideas as to what marks the end of input. scanf for example typically leaves a newline behind, and that's just what the others like to see as well.
The first bit of your code might look like this
ALWAYS use fgets() to read a line of input, then you can use array access, sscanf, or a whole bunch of other string functions to validate and convert the information.
For extra safety, check the return result of fgets() as well.
> exit=1;
This is also the name of a function in stdlib.h
Try to avoid such names to avoid later confusion.
This is wrong, main returns int.
http://c-faq.com/ansi/voidmain.html
http://faq.cprogramming.com/cgi-bin/...&id=1043284376
> gets()
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
And while I'm on a roll, try this as well
http://www.catb.org/~esr/faqs/smart-...html#writewell
The real problem is you're mixing scanf() with gets() (ugh!!!) and getchar()
If you mix and match, as you're doing, then you'll come unstuck. The reason being they have different ideas as to what marks the end of input. scanf for example typically leaves a newline behind, and that's just what the others like to see as well.
The first bit of your code might look like this
char buff[BUFSIZ];
char choice;
while (exit)
{
printf("Please choose one of the options below:\n"
"1- Create/update class. \n"
"2- Delete class. \n"
"3- Insert new species. \n"
"4- Delete species. \n"
"5- Update species. \n"
"6- Print species. \n"
"7- Print class. \n"
"8- Exit. \n");
fgets( buff, sizeof buff, stdin );
choice = buff[0];
// sscanf( buff, "%c", &choice ); // would also workFor extra safety, check the return result of fgets() as well.
> exit=1;
This is also the name of a function in stdlib.h
Try to avoid such names to avoid later confusion.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Did you replace ALL your other usage of gets() and scanf() with fgets() and sscanf() ?
If you didn't, you haven't really solved anything yet.
If you didn't, you haven't really solved anything yet.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
•
•
Join Date: Mar 2005
Location: Phnom Penh, Cambodia
Posts: 410
Reputation:
Rep Power: 5
Solved Threads: 36
Off-topic:
Why declare one element array of char?
Why declare one element array of char?
char class_name[NAME_LENGTH],choice[1]; Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
•
•
Join Date: Dec 2007
Posts: 9
Reputation:
Rep Power: 0
Solved Threads: 0
Well....a good input for my case is: a number between 1-8-> a number and then whatever but not some char and then the number....means i need to check only the [0] place...
i also tried choice[100] with the same result.......and in the example program i can insert: "dhgfdshjdhgdgdhg fdhgfdhgfdhnfnlnlnnlknl" means not limitd length of string....
And i can't use things like fgets or "buff", i don't know what it is really......
I really don't get it, why it skips the gets after the first input.....
1 action
Menu
Input...
2 action
Menu
! Errror message (default choice)
Menu !
Input
i also tried choice[100] with the same result.......and in the example program i can insert: "dhgfdshjdhgdgdhg fdhgfdhgfdhnfnlnlnnlknl" means not limitd length of string....
And i can't use things like fgets or "buff", i don't know what it is really......
I really don't get it, why it skips the gets after the first input.....
1 action
Menu
Input...
2 action
Menu
! Errror message (default choice)
Menu !
Input
•
•
Join Date: Mar 2005
Location: Phnom Penh, Cambodia
Posts: 410
Reputation:
Rep Power: 5
Solved Threads: 36
•
•
•
•
Well....a good input for my case is: a number between 1-8-> a number and then whatever but not some char and then the number....means i need to check only the [0] place...
•
•
•
•
i also tried choice[100] with the same result.......and in the example program i can insert: "dhgfdshjdhgdgdhg fdhgfdhgfdhnfnlnlnnlknl" means not limitd length of string....
This piece of code should works fine.
cplusplus Syntax (Toggle Plain Text)
char choice; do { printf("Please choose one of the options below:\n" "1- Create/update class. \n" "2- Delete class. \n" "3- Insert new species. \n" "4- Delete species. \n" "5- Update species. \n" "6- Print species. \n" "7- Print class. \n" "8- Exit. \n"); choice = getch(); switch(choice) { case '1': printf("This is create/update class part"); break; case '2': printf("This is Delete class"); break; //...... //...... } } while(choice != '8');
Useful Trick
To track the non-syntax errors, you can insert the printf() in the code you think that it is the cause to show what is really happened. For example (from your code)
while (exit)
{
printf("Please choose one of the options below:\n"
"1- Create/update class. \n"
"2- Delete class. \n"
"3- Insert new species. \n"
"4- Delete species. \n"
"5- Update species. \n"
"6- Print species. \n"
"7- Print class. \n"
"8- Exit. \n");
//scanf("%d", &flag);
gets(choice);
printf("%c", choice[0]);
//choice=getchar();
//scanf("%c",&choice[0]);
switch (choice[0])
{ Last edited by invisal : Dec 31st, 2007 at 9:43 am.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
> gets(choice);
Perhaps you should read the FAQs as well.
> I really don't get it, why it skips the gets after the first input.....
I told you already, mixing input methods is a disaster waiting to happen.
> And i can't use things like fgets or "buff", i don't know what it is really......
What do you mean you "don't know" ?
Are you supposed to restrict yourself to the few functions 'teacher' has told you about so far?
Perhaps you should read the FAQs as well.
> I really don't get it, why it skips the gets after the first input.....
I told you already, mixing input methods is a disaster waiting to happen.
> And i can't use things like fgets or "buff", i don't know what it is really......
What do you mean you "don't know" ?
Are you supposed to restrict yourself to the few functions 'teacher' has told you about so far?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
•
•
Join Date: Dec 2007
Posts: 9
Reputation:
Rep Power: 0
Solved Threads: 0
Well....YES...it's stupid i know, but that's how it works....and they are very strict about that.
THANX for your help people...appriciate it....will try to implement some of the things you told me....But it's really a small piece of the program.....nearly no points.....The real assignment is linked lists and it's a pain in the butt so far......working hard on that....
For someone who only learning C for a couple of months 5 hours peer week it's a big assignment.....i hadn't used pointers yet in my previous homework and now it's really confusing me....these one and another assignment and then the big final exam! BUT I MUST SAY I GOT TO REALLY LOVE IT......it's fun bashing your head against the wall sometimes trying to write a piece of code....
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Input check problem with numbers (C++)
- Check windows passwords (C)
- reading just a newline from the input (C++)
- Checkbox Input Problem (ASP)
- how to reverse a numbers input by user (C)
- Text File Input and manipulation (C++)
- Please check and provide solution for my code (ASP)
- need input or suggestions for new website-thx (Website Reviews)
- After formatting, PC REALLY, REALLY slow (Troubleshooting Dead Machines)
Other Threads in the C Forum
- Previous Thread: re directing data from serial port to ps/2 port
- Next Thread: function changing order while executing program



Linear Mode