GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Nov-2003, 05:11
belludandy belludandy is offline
New Member
 
Join Date: Nov 2003
Posts: 2
belludandy is an unknown quantity at this point

problem with C code


hi, this program will first input , when press '1',
it will goto mStduent() and then it should ask for another input,
but it didnt , it just stop after this line: printf("press 1 again");
why?
can anyone help, thx
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

char studentinput;
char input;
int i;

void mStudent(char *studentinput);
void mMainMenu(char *input);

main()
{
mMainMenu(&input);
}

void mMainMenu(char *input)
{
	printf("press 1");

                i = getchar(input);
	
	switch(i)
	{
		case '1': mStudent(&studentinput);
			break;

		case 'x': exit(-1);
			break;
	}
}

void mStudent(char *studentinput)
{
	
	printf("press 1 again");

	scanf("%c",&*studentinput);

	switch(*studentinput)
	{
		case '1':printf("select by id");
		break;

		case 'p':mMainMenu(&input);
		break;

		case 'x':exit(-1);
		break;
	}
}
  #2  
Old 26-Jan-2004, 21:28
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
NOTE:

In scanning through this forum, I noticed several threads that were not responded to. Even though, I am sure that these people have found answers, I thought it might be nice to post some solutions for people browsing these threads at a later date. It will jumble these threads a little and if it irritates anyone please let me know. Also, please note that I am fairly entrenched with C (as opposed to C++). Most of my solutions are posted in C.


Here are some problems that I noticed with this program.
  1. At this point, stdlib is not necessary for this program.
  2. The use of the global variables is confusing. As a rule global variables should be avoided, but if you are going to use them, there is no need to pass them as variables.
  3. I am a big fan of using functions to seperate the program, but in this case, I would just put the contents of your mMainMenu program into the main function.
  4. It is always a good idea to include a default statement in your switch structures.
  5. I see no need to pass the studentinput variable to your mStudent function, especially as a pointer. Only use this if you want the change to your variable be passed back to your calling function.
  6. The syntax of "&*studentinput" is counter productive. You are asking C for the address of the contents pointed to by the address. Simply putting "studentinput" does the exact same thing. In my revised program, I have declared studentinput as a char and therefore, it is necessary to to pass "&studentinput".
  7. It is not a good idea to embed calls to functions unless that is really what you want to do. Calling your mainmenu function from the mStudent function is not good syntax as you are just using program memory needlessly because you never exit any of your functions. You can get an infinite chain like:
    mMainmenu --> mStudent --> mMainmenu --> mStudent and so on.
  8. And finally, the main problem. When you read a char from the console it gets a bit tricky. You actually need to read in a string. Otherwise the carriage return charecter is in the input buffer and will be the next charecter returned. Instead I use a small string (leaving enough room for at least the CR and NULL) I also use the function fgets to avoid memory over-runs.

Anyway here is a modified program. This is a start to a decent template to use for a console menu driven program:
CPP / C++ / C Code:
#include <stdio.h>
#include <malloc.h>

char mStudent()
{

	char*	input = (char*) malloc(5);

	do{
		printf("press 1 again: ");
		fgets(input,4,stdin);

		switch(*input){
    		case '1':
				printf("select by id\n");
    			break;
			case 'p':
				break;
			case 'x':
				return('x');
				break;
			default:
				printf("mStudent: %c is an invalid input.  Try again\n",input);
				break;
		}
	}while(*input!='p');
	return('p');
}


int main()
{
	char*	input = (char*) malloc(5);

	do{
		printf("press 1: ");
		fgets(input,4,stdin);

		switch(*input){
			case '1':
				*input = mStudent();
				break;
			case 'x':
				break;
			default:
				printf("%c is an invalid input.  Try again\n",input);
				break;
		}
	}while(*input!='x');

	return 0;
}
 
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
folder problem in trees zuzupus MySQL / PHP Forum 23 26-Sep-2003 07:32
link problem [1][2]----- zuzupus MySQL / PHP Forum 0 16-Sep-2003 05:16
join problem zuzupus MySQL / PHP Forum 0 14-Aug-2003 05:11
Code problem (a newbie question) monkster87 CPP / C++ Forum 3 11-Aug-2003 12:46
cols width problem zuzupus Web Design Forum 4 23-Jul-2003 06:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:07.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.