1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C calc problem

Discussion in 'C' started by fsakalos, May 14, 2007.

  1. fsakalos

    fsakalos New Member

    Joined:
    May 14, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Slovakia
    Code:
    #include <stdio.h>
    int main()
    {
    int letter;
    printf("Vyber si + alebo - alebo * alebo 2(druha mocnina): ");
    letter = getchar();
    	if(letter == '+'){
    		float sucet, citatel, menovatel;
    			printf("Zadaj scitanec: ");
    			scanf("%g", &citatel);
    			printf("Zadaj scitanec: ");
    			scanf("%g", &menovatel);
    			sucet = citatel + menovatel;
    			printf("Vysledok je: %g\n", sucet);
    			return 0;
    	}
    	else if(letter == '-'){
    		float rozdiel, odcitatel, odcitanec;
    			printf("Zadaj odcitatel: ");
    			scanf("%g", &odcitatel);
    			printf("Zadaj odcitanec: ");
    			scanf("%g", &odcitanec);
    			rozdiel = odcitatel - odcitanec;
    			printf("Vysledok je %g\n", rozdiel);
    	return 0;
    	}
    	else if(letter == '*'){
    		float nasobok1, nasobok2, sucin;
    			printf("Zadaj prvy nasobok: ");
    			scanf("%g", &nasobok1);
    			printf("Zadaj druhy nasobok: ");
    			scanf("%g", &nasobok2);
    			sucin = nasobok1 * nasobok2;
    			printf("Vysledok je %g\n", sucin);
    	return 0;
    	}
    	[COLOR=Red]else if(letter == '2'){
    		int hovno;
    		printf("Chcete vediet konkretnu mocninu(2) alebo tabulku od x po 100?(t) ");
    		hovno = getchar();
    			if(hovno == 't'){
    				printf("\nsi pako\n");
    				return 0;
    			}
    			else if(hovno == '5'){
    				float mocnitel, vysledok;
    				printf("Zadaj mocnitel: ");
    				scanf("%g", &mocnitel);
    				vysledok = mocnitel * mocnitel;
    				printf("Vysledok je %g\n", vysledok);
    				return 0;
    			}[/COLOR]
    	return 0;
    	}
    	else {
    		printf("Zadaj jeden zo znakov ktore su napisane vo vete!!\n");
    		
    	
    	}
    
    
    }
    I'm writing this C calc and I wanted to do something like this: You get a possibility to select + or - or * or 2(power of 2). And then I started to write code that when you press 2, then you have posibillity to again select something. But this I cannot do(program doesn't give me any possibility and it just continue to printf). I've been looking over Internet but I cannot find anything about my problem. Red marked code is probably the problem
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    You need to put the flushing statement before the getchar() I dont remember the exact name of the function but its probably is fflush(stdin);
     
  3. fsakalos

    fsakalos New Member

    Joined:
    May 14, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Slovakia
    Do I need a special library for it, or stdio.h is enough?
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    fflush (stdin) will work with some compilers and not others. Use rewind (stdin). Stdio.h covers it.

    You are courting more crashes and endless looping by using scanf without testing it's return. Check your scanf docs for how to check the return to see if you actually scanned AND CONVERTED all items which you requested.
     
  5. fsakalos

    fsakalos New Member

    Joined:
    May 14, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Slovakia
    I've solved the problem. Before hovno = getchar(); should be getchar(); so it should look like this:

    getchar();
    hovno = getchar();

    But anyway thanks for help and sorry for duplication. :eek:
     
  6. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    That solution will work if there's only ONE extra char in the buffer. Your user (who may not be you) may pound a few extras in there. You want to get them all with rewind.
     
  7. fsakalos

    fsakalos New Member

    Joined:
    May 14, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Slovakia
    It doesn't work with rewind(stdin);
     
  8. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    My apologies, use what works, then. I'm going on the usual cause of failures because I can't actually read what the user is supposed to be doing.
     
  9. fsakalos

    fsakalos New Member

    Joined:
    May 14, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Slovakia
    OK, thanks for help
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    Then does the fflush works
     
  11. fsakalos

    fsakalos New Member

    Joined:
    May 14, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Slovakia
    FFLUSH doesn't work too. I have tried both.
     
  12. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    See your other post for a basic description of C/C++ input. Given that, and a thorough understanding of what you are asking your user to do and asking your code to do may help you solve your problem.

    If you translated your prompts into English and described what you actually want, and described the conditions under which your program fails, I could probably help you out. I apologize for not being able to do that, thus far.
     
  13. fsakalos

    fsakalos New Member

    Joined:
    May 14, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Slovakia
    I will for sure do this but now I work on three programs so I have lack of time.
     

Share This Page