By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

Multiple function selection

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a


On 11/26/2003 11:25 AM, Les Coover wrote:
I have tried writing the selection process after <snip> if (select = 1)


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

Ed.

Nov 13 '05 #2

P: n/a

"Ed Morton" <mo****************@lucent.com> wrote in message
news:3F**************@lucent.com...


On 11/26/2003 11:25 AM, Les Coover wrote:
I have tried writing the selection process after <snip>
if (select = 1)


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
Nov 13 '05 #3

P: n/a

"Les Coover" <lc******@cox.net.spam> wrote in message
news:Cl6xb.41778$yJ.5251@okepread02...

"Ed Morton" <mo****************@lucent.com> wrote in message
news:3F**************@lucent.com...


On 11/26/2003 11:25 AM, Les Coover wrote:
I have tried writing the selection process after

<snip>
if (select = 1)


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

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
Nov 13 '05 #4

P: n/a
Les Coover wrote:

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.

--
Er*********@sun.com
Nov 13 '05 #5

P: n/a
In <Gk5xb.41503$yJ.18575@okepread02> "Les Coover" <lc******@cox.net.spam> writes:
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.
query_data();
else
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6

P: n/a
Dan Pop wrote:
In <Gk5xb.41503$yJ.18575@okepread02> "Les Coover" <lc******@cox.net.spam> writes:


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.


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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Nov 13 '05 #7

P: n/a
Kevin Goodsell wrote:
Dan Pop wrote:
In <Gk5xb.41503$yJ.18575@okepread02> "Les Coover"
<lc******@cox.net.spam> writes:


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.

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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Nov 13 '05 #8

P: n/a
In <HP*****************@newsread1.news.pas.earthlink. net> Kevin Goodsell <us*********************@neverbox.com> writes:
Dan Pop wrote:
In <Gk5xb.41503$yJ.18575@okepread02> "Les Coover" <lc******@cox.net.spam> writes:


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.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.