Multiple function selection

L

Les Coover

I have tried writing the selection process after
scanf many different ways. No matter what
is selected program execution goes to the
add_data function. How can I get this to work?

/* em_at01.c */

#include <stdio.h>

void add_data(void);
void print_data(void);
void query_data(void);

int main(void)
{
int select;
printf("\nWHAT DO YOU WANT TO DO?\n\n");
printf("%s\n\n", "SELECTION PROCEDURE");
printf("%s\n", " 1 Add Record/s");
printf("%s\n", " 2 Print Database");
printf("%s\n", " 3 Query Database");
printf("%s\n", " Ctrl + C Exit");

scanf("%d", select);
if (select = 1)
add_data();
else if (select = 2)
print_data();
else if (select = 3)
query_data();
else

return 0;
}

void add_data(void)
{
printf("this is add_data (void)");
}

void print_data(void)
{
printf("this is print_data (void)");
}

void query_data(void)
{
printf("this is query_data (void)");
}

Thanks, Les
 
L

Les Coover

Ed Morton said:
NOTE: you mean "==", not "=". By the way, you may want to use a switch statement
instead of a chain of "if"s here....

Ed.

Tried

scanf("%d", select);
if (select == 1)
add_data();
else if (select == 2)
print_data();
else if (select == 3)
query_data();
else

Wont call any function, focus goes back to prompt.

Tried

scanf("%d", select);
switch (select)
{
case 1:
add_data();
break;
case 2:
print_data();
break;
case 3:
query_data();
break;
}

does same thing.

Even tried

switch (select)
{
case 1:
add_data();

case 2:
print_data();

case 3:
query_data();

}

Same thing.

Les
 
D

Dan Pop

In said:
I have tried writing the selection process after
scanf many different ways. No matter what
is selected program execution goes to the
add_data function. How can I get this to work?

Ever considered reading a C book?

There are several mistakes in your program and all of them are of the
most basic kind. You really have no clue about C.

This homework assignment really begs for an array of function pointers,
but you have plenty of other, most basic things, to learn until then.
/* em_at01.c */

#include <stdio.h>

void add_data(void);
void print_data(void);
void query_data(void);

int main(void)
{
int select;
printf("\nWHAT DO YOU WANT TO DO?\n\n");
printf("%s\n\n", "SELECTION PROCEDURE");
printf("%s\n", " 1 Add Record/s");
printf("%s\n", " 2 Print Database");
printf("%s\n", " 3 Query Database");
printf("%s\n", " Ctrl + C Exit");

scanf("%d", select);

Wrong. Also no attempt to check whether the function call succeeded.
if (select = 1)
Wrong.

add_data();
else if (select = 2)
Wrong.

print_data();
else if (select = 3)
Wrong.


Wrong.

return 0;
}

void add_data(void)
{
printf("this is add_data (void)");
Wrong.

}

void print_data(void)
{
printf("this is print_data (void)");
Wrong.

}

void query_data(void)
{
printf("this is query_data (void)");
Wrong.

}

Do yourself a favour and compile your code with gcc -Wall -O. It would
have caught and reported many of your mistakes.

Dan
 
L

Les Coover

Les Coover said:
Tried

scanf("%d", select);
if (select == 1)
add_data();
else if (select == 2)
print_data();
else if (select == 3)
query_data();
else

Wont call any function, focus goes back to prompt.

Tried

scanf("%d", select);
switch (select)
{
case 1:
add_data();
break;
case 2:
print_data();
break;
case 3:
query_data();
break;
}

does same thing.

Even tried

switch (select)
{
case 1:
add_data();

case 2:
print_data();

case 3:
query_data();

}

Same thing.

Les
Thanks for help Ed, here is how I solved the problem:

/* em_atr03.c */

#include <stdio.h>
#include <string.h>

void add_data(void);
void print_data(void);
void query_data(void);

int main(void)
{
int select;
char color[100];

printf("\nWHAT DO YOU WANT TO DO?\n\n");
printf("%s\n\n", "SELECTION PROCEDURE");
printf("%s\n", " yellow Add Record/s");
printf("%s\n", " green Print Database");
printf("%s\n", " blue Query Database");
printf("%s\n", " Ctrl + C Exit");

scanf("%99s", color);
if(strcmp(color, "yellow")== 0)
add_data();
if(strcmp(color, "green")== 0)
print_data();
if(strcmp(color, "blue")== 0)
query_data();
else
return 0;
}

void add_data(void)
{
printf("this is add_data (void)");
}

void print_data(void)
{
printf("this is print_data (void)");
}

void query_data(void)
{
printf("this is query_data (void)");
}

Les
 
E

Eric Sosman

Les said:
Tried

scanf("%d", select);
if (select == 1)
add_data();
else if (select == 2)
print_data();
else if (select == 3)
query_data();
else

Wont call any function, focus goes back to prompt.
[...]

First, you should check whether the scanf() call
actually converted anything. You've told it to expect
a number, but if it instead encounters a Q or a $ or
some other non-numeric garbage it will be unable to
produce a value for `select'. The value returned by
the scanf() function tells you how many items were
successfully converted; in your case, any value other
than 1 means there was trouble.

Second, when scanf() *does* encounter trouble of
this sort, it can be tricky to recover from it and
proceed. You can't just re-issue the scanf() call,
because the unconvertable garbage is still sitting
there, unconsumed, and will bollix the second call
just as surely as it bollixed the first. scanf()
looks easy, but it's difficult to use for interactive
input. See Question 12.19 (in fact, see all of 12.12
through 12.20) in the comp.lang.c Frequently Asked
Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

Finally, whenever you're confronted with some
inexplicable behavior in your program it's often helpful
to take a look at the values the program is actually
chewing on, as opposed to the values you *thought* it
was ingesting. One way to do this is with a debugger;
my personal feeling is that debuggers are overkill for
simple problems, and that you can do at least as well
by adding a few printf() or fprintf() calls at strategic
points in the program. In your case, `select' is clearly
the variable directly involved in whatever's going on, so
just after reading it and before using it I suggest you

printf ("You chose (I think) %d\n", select);

In situations like this, the surprising outputs are the
most illuminating.
 
K

Kevin Goodsell

Dan said:
Wrong. Also no attempt to check whether the function call succeeded.

The bigger problem here, which I only just noticed, is that he's passing
an int where scanf expects a pointer to int.

scanf("%d", &select);

-Kevin
 
K

Kevin Goodsell

Kevin said:
The bigger problem here, which I only just noticed, is that he's passing
an int where scanf expects a pointer to int.

scanf("%d", &select);

Sorry, I misread that. You caught the error I mentioned, and then went
on to describe a second error. I thought you were only talking about the
failure to check the return value.

-Kevin
 
D

Dan Pop

In said:
The bigger problem here, which I only just noticed, is that he's passing
an int where scanf expects a pointer to int.

scanf("%d", &select);

Why do you think I wrote "wrong"?

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top