please help quick, brainfart moment should be easy solution

This is a discussion on please help quick, brainfart moment should be easy solution within the C Programming forums, part of the General Programming Boards category; Ok, this is kinda embarassing but im having trouble with the beginning of my code. i need help quickly to ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    8

    please help quick, brainfart moment should be easy solution

    Ok, this is kinda embarassing but im having trouble with the beginning of my code. i need help quickly to get this project done. The code is supposed to display the options below, (1-5) and then scan for user input. instead my program is just endless loooping without scanning. any help? i think a new persepctive is all i need.
    ive posted the problem bit below.


    Code:
    int main () {
    	flowratetable.name==NULL;
    	userinterface();
    }
    
    
    void userinterface(){
    	int quit=0, exit=0;
    	int option=0;
    	while (quit==0){
    	printf("\t1. Create a flow rate table\n\t2. Load an existing flow rate table\n\t3. Display currenty loaded flow rate table\n\t4. Estimate water depth using flow rate table\n\t5. Terminate\n\n");
    	if (flowratetable.name != NULL ){
    		printf("Table %s Loaded", flowratetable.name);
    	}
    	else if (flowratetable.name == NULL){
    		printf("No Table Loaded");
    		while (exit=0){
    			scanf("%i\n", &option);
    			if (option==1){
    				addflowratetable();}
    			if (option==2){
    			loadflowratetable();}
    			if (option==3){
    			displayflowratetable();}
    			if (option==4){
    			estimatewaterdepth();}
    			if (option==5){
    				printf("Terminating\n");
    			quit=1;}
    		}
    	}}}

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    while(exit=0)

    you really need to start compiling with ALL WARNINGS on.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    i fixed that but it still keeps infinite looping...

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well of course it is going to be looping since exit will always be 0. You never change it. That's another warning you should have gotten.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    25
    I also recommend changing the variable name. It's alright to name it exit, but exit is also the name of a function exit(0); in one of the standard libraries. It could get confusing, so better to practice not using exit as a name.

    The other posters have posted helpful tips!

    I would like to offer an idea. Instead of looping in a weird while loop, I think looping within in infinite for loop is better, and put in a case that can break the loop. (you still need to define when exit isn't 0 though, as one of the users pointed out)

    Code:
    for (;;){
     // some code
    
     printf("Do you want to exit this weird loop? Enter something...Yes = >0, No = 0.");
     scanf("%d", &exit);
     if (exit != 0)
       break;
    }
    Of course you don't need to ask the user, you can set it up so the program knows when to switch exit to something >0. You could also use character Y or y, or N or n, and loop while the user doesn't press N or n when asked if they want to exit. Makes more sense than simply looping while something is 0.

    You need to be careful with break because sometimes it can set you back to a place where you may not want to be. It is good fun to use though! You can also use more advanced stuff like setjmp and longjmp but I think those are unneeded in most cases and probably frowned upon, along with goto statements.
    Last edited by tenchu; 12-05-2010 at 09:57 PM.

  6. #6
    Banned
    Join Date
    May 2009
    Posts
    37
    not because when you WERE a beginner and wrote codes (loops) this way, doesn't mean those you condescend do write them like this. i've seen dozens of functions with more than 1600 lines of codes, without any comments, mostly in giant loops, that you practically have to keep the whole function in your head, to see it from the large scale until the smallest, most core details. and this is just in one side of the project....

    i know this is against my code to reveal insights, but there are people out there that will only speak of trivial things, as they are careful not to reveal trade secrets.

    it's funny how "nerdy" people just get to programming as a "side-dish", and see how it works and thinks whatever works is just as good as any solutions. yet put them in a real computational complexity lecture hall and watch their dumbfounded faces as they see why how they've been doing things are very inefficient.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime problem
    By SantoshN in forum C Programming
    Replies: 2
    Last Post: 10-12-2010, 03:42 PM
  2. char input and recognition - easy solution to help a newbie!
    By jakethehake in forum C++ Programming
    Replies: 5
    Last Post: 12-17-2009, 11:19 AM
  3. cant see solution, should be easy array problem.
    By jinn in forum C Programming
    Replies: 4
    Last Post: 10-30-2007, 06:45 PM
  4. NEED Quick Standard Solution!
    By JarJarBinks in forum C++ Programming
    Replies: 4
    Last Post: 11-07-2004, 11:53 PM
  5. Quick Easy Question
    By St0rmTroop3er in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 02-24-2004, 01:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21