2 Replies - 696 Views - Last Post: 30 October 2014 - 12:58 PM Rate Topic: -----

#1 498201   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-October 14

Switch case not working on Linux but works on Windows

Posted 17 October 2014 - 07:55 PM

Hello.

I made this code on my school computers on Dev-C++ and it worked like a charm there, but in my computer (OS: Fedora 20). When I compile with gcc on my terminal (# gcc main.c -o Numbers) and then execute it (# ./Numbers) the program closes automatically after the 3rd printf (p ("Dime que operación vas a realizar \n \t");)/>

#include <stdio.h>
#include <stdlib.h>
#define p printf
#define s scanf

int n1, n2;
char o;

main (void)
{
	p ("Dame el primer número \n \t");
	s ("%i",&n1);
	p ("Dame el segundo número \n \t");
	s ("%i",&n2);
	
	p ("Dime que operación vas a realizar \n \t");
	s ("%c",&o);
	
	switch(o)
	{	
		case 's':
			{
				p ("La suma de %i y %i es %i \n",n1,n2,n1+n2);
			}
			break;
		case 'r':
			{
				p ("La resta de %i y %i es %i \n",n1,n2,n1-n2);
			}
			break;
		case 'm':
			{
				p ("La multiplicación de %i y %i es %i \n",n1,n2,n1*n2);
			}
			break;
		case 'd':
			{
				switch(n2)
				{
					case 0:
						{
							p ("No se puede dividir entre %i",n2);
						}
						break;
					default:
						{
							p ("La división de %i y %i es %i \n",n1,n2,n1/n2);
						}
						break;
				}
			}
			break;
	}
	p ("Da click en \"Enter\" para continuar\n");
	fflush(stdin);
	getchar();
	fflush(stdin);
	return 0;	
}



I honestly don't see what is wrong, thanks in advance :)/>

Is This A Good Question/Topic? 0
  • +

Replies To: Switch case not working on Linux but works on Windows

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Switch case not working on Linux but works on Windows

Posted 17 October 2014 - 08:25 PM

Well it's pretty hard to see what you're doing wrong with all those ridiculous macros. My first suggestion is to stop using this type of macro substitution and just use the actual function names.

Next I suggest you change your third scanf() call and add a space in front of the specifier so you skip leading whitespace.

:
scanf(" %c", yourChar);


The problem is the end of line character being left behind by the previous scanf() calls.

Edit: Also on Linux fflush() of an input stream does absolutely nothing. This function is only guaranteed to work with an output stream, using it on an input stream produces undefined behavior.


Jim

This post has been edited by jimblumberg: 17 October 2014 - 08:40 PM

Was This Post Helpful? 2
  • +
  • -

#3 498201   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-October 14

Re: Switch case not working on Linux but works on Windows

Posted 30 October 2014 - 12:58 PM

View Postjimblumberg, on 17 October 2014 - 08:25 PM, said:

Well it's pretty hard to see what you're doing wrong with all those ridiculous macros. My first suggestion is to stop using this type of macro substitution and just use the actual function names.

Next I suggest you change your third scanf() call and add a space in front of the specifier so you skip leading whitespace.

:
scanf(" %c", yourChar);


The problem is the end of line character being left behind by the previous scanf() calls.

Edit: Also on Linux fflush() of an input stream does absolutely nothing. This function is only guaranteed to work with an output stream, using it on an input stream produces undefined behavior.


Jim


Thank you very much, Jim I managed to solve it the way you told me :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1