473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Multiple function selection

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
8 2553


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

"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

"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Disco-181 | last post by:
Hi, I have a script which isn't working in Mozilla based browser for some reason. I'm trying to run two functions from the body onload tag and it simply isn't happening. I have a cascading...
0
by: B | last post by:
Using Access2000, the sample code below is what I have been modifying and working on since the past week and I could not get it to work properly. What I wanted to accomplish: 1) read from a...
5
by: Lie | last post by:
Hi all, I have problem in getting selectedindex of multiple listbox selection in a datagrid. I have a listbox with multiple selection mode inside datagrid. In Edit mode, I need to get back all...
2
by: bienwell | last post by:
Hi all, I've got a problem with getting the data values from multiple selections in the list box. I could only get the value and the index of the first item from the selection, but not the...
10
by: ads | last post by:
hi, after binding the dropdownlist to a datasource, ive experience this error "Cannot have multiple items selected in a dropdownlist" after using the code:...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
18
by: =?Utf-8?B?TGkgV2VuZw==?= | last post by:
Hi, Is there a way for TreeView to have multiple selections? But I am not talking about its checked boxes. I want a way similar to ListView with MultiSelect = True. So I can use or key and...
92
by: bonneylake | last post by:
Hey Everyone, Well i was hoping someone could explain the best way i could go about this. i have a few ideas on how i could go about this but i am just not sure if it would work. Right now i...
58
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also...
482
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if this is more of a coldfusion problem or a javscript problem. So if i asked my question in the wrong section let me know an all move it to the correct place. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.