Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Multiple function selection

Reply
Thread Tools

Multiple function selection

 
 
Les Coover
Guest
Posts: n/a
 
      11-26-2003
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


 
Reply With Quote
 
 
 
 
Ed Morton
Guest
Posts: n/a
 
      11-26-2003


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.

 
Reply With Quote
 
 
 
 
Les Coover
Guest
Posts: n/a
 
      11-26-2003

"Ed Morton" <> wrote in message
news:...
>
>
> 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


 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      11-26-2003
In <Gk5xb.41503$yJ.18575@okepread02> "Les Coover" <> 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:
 
Reply With Quote
 
Les Coover
Guest
Posts: n/a
 
      11-26-2003

"Les Coover" <> wrote in message
news:Cl6xb.41778$yJ.5251@okepread02...
>
> "Ed Morton" <> wrote in message
> news:...
> >
> >
> > 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


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-26-2003
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.

--

 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      11-26-2003
Dan Pop wrote:

> In <Gk5xb.41503$yJ.18575@okepread02> "Les Coover" <> 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.

 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      11-26-2003
Kevin Goodsell wrote:

> Dan Pop wrote:
>
>> In <Gk5xb.41503$yJ.18575@okepread02> "Les Coover"
>> <> 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.

 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      11-27-2003
In <HP7xb.15861$. net> Kevin Goodsell <> writes:

>Dan Pop wrote:
>
>> In <Gk5xb.41503$yJ.18575@okepread02> "Les Coover" <> 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:
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
selection structure nested within another selection structure 4Ankit@gmail.com Javascript 1 12-07-2006 11:47 AM
JTable with row selection, but no cell selection Simon Niederberger Java 2 01-07-2005 04:17 PM
JS comparing innerHTML to text selection (window.getSelection() /document.selection) Andrew Crowe HTML 1 09-13-2004 02:22 PM
How to change a range selection to text selection? Loebb Javascript 0 02-23-2004 02:12 PM
HOWTO autopost the selection list upon selection curiousity ASP .Net Mobile 0 11-21-2003 12:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57