Thread: Problem with my code

  1. #1
    Registered User
    Join Date
    Sep 2009
    Location
    Netherlands
    Posts
    17

    Problem with my code

    Hello,

    I just started with programming C. My first task is to code a simple system that ask a question that you can answer with y (yes) or n (no) and goes to the next question. And at the end gives a statement of what can be the problem.

    This is my code:
    Code:
    #include <stdio.h>
    void main(void)
    {
    	char answer_1;
    	char answer_2;
    	char answer_3;
    	char answer_4;
    	char answer_5;
    	char answer_6;
    
    	printf("Maakt de PC enig geluid?\n");
    	scanf("%c",&answer_1);
    		if(answer_1=='n') 
    		{
    			printf("Zit de stekker erin?\n");
    			scanf("%c",&answer_2);
    					if(answer_2=='n')
    					{
    						printf("Pffffff......\n");
    					}
    					if(answer_2=='y')
    					{
    						printf("Controleer de voeding (zekering kapot?)\n");
    					}
    		}
    		else
    		{	
    			printf("Is er geen beeld op het scherm te zien?\n");
    			scanf("%c",&answer_3);
    					if(answer_3=='n')
    					{
    						printf("Geen 'memory errors' tijdens booten?\n");
    						scanf("%c",&answer_4);
    							if(answer_4=='n')
    							{
    								printf("Controleer/vervang SIM geheugenkaarten\n");
    							}
    							else
    							{
    								printf("Zijn er meldingen over hard disk errors tijdens het booten?\n");
    								scanf("%c",&answer_5);
    									if(answer_5=='n')
    									{
    										printf("Run hardwarediagnoseprogramma. Is alles OK?\n");
    										scanf("%c",&answer_6);
    											if(answer_6=='n')
    											{
    												printf("Vervang het foutieve onderdeel.\n");
    											}
    											else
    											{
    												printf("Softwareprobleem: herinstalleer windows\n");
    											}
    									}
    									else
    									{
    										printf("Controleer hard drives en kabels./n");
    									}
    							}
    					}
    					else
    					{ 
    						printf("Controleer videokaart, monitor en bedrading.\n");
    					}
    		}
    }
    When i compile it it gives no errors. When i start it it asks the first question and if i answer that with n it gives the second but then it goes to the end of the program in stead of giving the user the change to give new input and scan this. Can anyone tell me what there is wrong?

    Thanks for your help!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You want scanf(" %c") -- notice carefully the difference. %c by itself will read in any character, including enter-key (which you're going to type a lot of); the space will cause that to be skipped.

    (If you don't want people typing in "yes yes oh yes" then you're going to have to get even more careful with the input.)

  3. #3
    Registered User
    Join Date
    Sep 2009
    Location
    Netherlands
    Posts
    17
    Thanks for your help! I changed it and it seems to work fine now. I also tried it with switch function so it would give an error message when people tried to type something different. But I also forgot about the space there.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Location
    Netherlands
    Posts
    17

    New problem

    I got a new task. I should make a ALU simulator in C. It should:
    - read the hexadecimal values of 'a' and 'b'
    - perform alu operation
    - output must look like this example:

    32-bit ALU Simulator by Tom

    Please enter the values of operands a and b in hex : 01010101 ffffffff
    Enter operation code (And, Or, eXor, shift Left, shift Right, +, -) : a
    Hexadecimal result: 01010101 a ffffffff = 01010101
    Decimal result: 16843009 a 4294967295 = 16843009

    This is my code
    Code:
    #include <stdio.h>
    #include <math.h>
    
    void main (void)
    {
    	unsigned int a;
    	unsigned int b;
    	char c;
    	unsigned int and;
    	unsigned int or;
    	unsigned int exor;
    	unsigned int shiftleft;
    	unsigned int shiftright;
    	unsigned int add;
    	unsigned int subtract;
    
    
    	printf("32-bit ALU simulator by Tom.\n\n");
    	printf("Please enter the values of operands a and b in hex :\n");
    	scanf(" %x %x", &a, &b);
    
    	printf("Enter operation code (And, Or, eXor, shift Left, shift Right, +, -) :\n");
    	scanf(" %c", &c);
    
    	switch (c)
    	{
    		case 'a':									//and
    			and = a & b;
    			printf("Hexadecimal result: %x %c %x = %x", a,c,b,and);
    			break;
    		case 'o':									//or
    			or = a | b;
    			printf("Hexadecimal result: %x %c %x = %x", a,c,b,or);
    			break;
    		case 'x':									//exor
    			exor = a ^ b;
    			printf("Hexadecimal result: %x %c %x = %x", a,c,b,exor);
    			break;
    		case 'l':									//shift left
    			shiftleft = a << b;
    			printf("Hexadecimal result: %x %c %x = %x", a,c,b,shiftleft);
    			break;
    		case 'r':									//shift right
    			shiftright = a >> b;
    			printf("Hexadecimal result: %x %c %x = %x", a,c,b,shiftright);
    			break;
    		case '+':									//+
    			add = a + b
    			printf("Hexadecimal result: %x %c %x = %x", a,c,b,add);
    			break;
    		case '-':									//-
    			subtract = a - b
    			printf("Hexadecimal result: %x %c %x = %x", a,c,b,subtract);
    			break;
    		default:
    			printf("illegal operation code\n");
                            exit();
    			break;
    	}
    
    	
    }
    I have some questions:
    1. When i perform the and operation the printed result is 1010101. How can I correct that?
    2. How can i write the hexadecimal numbers to decimal numbers?
    3. The exit() gives an undeclared identifier error. How to solve that?

    Thanks in advance for your help!

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Tom.b View Post
    2. How can i write the hexadecimal numbers to decimal numbers?
    3. The exit() gives an undeclared identifier error. How to solve that?
    2) Illusory issue. An int is stored in binary. You can printf() it as %d or %x.
    3) exit() may want an argument. The argument is an integer, because the return value of main() should always be int, not void. Also, exit may require stdlib.h to work correctly. Since you are still in main() anyway, you do not need exit, you can use return. Just make sure you return an int

    WRT #1 you have a pile of uninitialized variables here so what this code is supposed to do I don't know, but I would not expect anything but undefined behavior.

    Code:
    	unsigned int and;
    	unsigned int or;
    	unsigned int exor;
    	unsigned int shiftleft;
    	unsigned int shiftright;
    	unsigned int add;
    	unsigned int subtract;
    Last edited by MK27; 09-18-2009 at 02:31 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Sep 2009
    Location
    Netherlands
    Posts
    17
    Thanks for your help. The task said: "Write a simulator for a 32-bit ALU in C. Use unsigned ints for input a,b and result.
    For ALU see this Arithmetic logic unit - Wikipedia, the free encyclopedia . My version should take two 32 bit binary words a,b and produces a single 32 bit binary world called result.

    new code:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main ()
    {
    	unsigned int a;
    	unsigned int b;
    	char c;
    	unsigned int and;
    	unsigned int or;
    	unsigned int exor;
    	unsigned int shiftleft;
    	unsigned int shiftright;
    	unsigned int add;
    	unsigned int subtract;
    
    
    	printf("32-bit ALU simulator by Tom Bruls.\n\n");
    	printf("Please enter the values of operands a and b in hex :\n");
    	scanf(" %x %x", &a, &b);
    
    	printf("Enter operation code (And, Or, eXor, shift Left, shift Right, +, -) :\n");
    	scanf(" %c", &c);
    
    	switch (c)
    	{
    		case 'a':									//and
    			and = a & b;
    			printf("Hexadecimal result: %x %c %x = %x\n", a,c,b,and);
    			printf("Decimal result: %d %c %d = %d\n", a,c,b,and);
    			break;
    		case 'o':									//or
    			or = a | b;
    			printf("Hexadecimal result: %x %c %x = %x\n", a,c,b,or);
    			printf("Decimal result: %d %c %d = %d\n", a,c,b,or);
    			break;
    		case 'x':									//exor
    			exor = a ^ b;
    			printf("Hexadecimal result: %x %c %x = %x", a,c,b,exor);
    			printf("Decimal result: %d %c %d = %d\n", a,c,b,exor);
    			break;
    		case 'l':									//shift left
    			shiftleft = a << b;
    			printf("Hexadecimal result: %x %c %x = %x\n", a,c,b,shiftleft);
    			printf("Decimal result: %d %c %d = %d\n", a,c,b,shiftleft);
    			break;
    		case 'r':									//shift right
    			shiftright = a >> b;
    			printf("Hexadecimal result: %x %c %x = %x\n", a,c,b,shiftright);
    			printf("Decimal result: %d %c %d = %d\n", a,c,b,shiftright);
    			break;
    		case '+':									//+
    			add = a + b;
    			printf("Hexadecimal result: %x %c %x = %x\n", a,c,b,add);
    			printf("Decimal result: %d %c %d = %d\n", a,c,b,add);
    			break;
    		case '-':									//-
    			subtract = a - b;
    			printf("Hexadecimal result: %x %c %x = %x\n", a,c,b,subtract);
    			printf("Decimal result: %d %c %d = %d\n", a,c,b,subtract);
    			break;
    		default:
    			printf("illegal operation code\n");
    			return (0);
    			break;
    	}
    
    	
    }

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Tom.b View Post
    Thanks for your help. The task said: "Write a simulator for a 32-bit ALU in C. Use unsigned ints for input a,b and result.
    I know what an ALU is. Your code does not do anything except read numbers from the input and then print them out again. You do not perform any bit manipulation or arithmetic (which is what the ALU does).

    What do you consider the purpose of the uninitialized variables I referred to in my last post (that was an edit, I dunno if you saw it) to be?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Uninitialized variables are fine, as long as you give them a value before you use them, which you do, so I don't know what MK is complaining about. (Well, I might: the idea is that you just one need one variable called, oh I don't know, result, that you can use in each case rather than one special variable for each operation.)

    To get printf to do what you want, why don't you read the manual on printf? (As in, type "man printf" into your terminal, or in that handy little box in the top right-hand corner of your web browser, and read how to make the formatted print statement printf print to your format.)

  9. #9
    Registered User
    Join Date
    Sep 2009
    Location
    Netherlands
    Posts
    17
    I see i forgot about the ' '. But now i get errors beginning from shiftleft saying:
    error C2015: too many characters in constant
    And why does this not perform any bit operation? What do I need to change for that?

    Maybe these questions sound silly to you but I really don't see what I am doing wrong.
    Code:
    int main ()
    {
    	unsigned int a;
    	unsigned int b;
    	char c;
    	unsigned int and;
    	unsigned int or;
    	unsigned int exor;
    	unsigned int shiftleft;
    	unsigned int shiftright;
    	unsigned int add;
    	unsigned int subtract;
    
    
    	printf("32-bit ALU simulator by Tom Bruls.\n\n");
    	printf("Please enter the values of operands a and b in hex :\n");
    	scanf(" %x %x", &a, &b);
    
    	printf("Enter operation code (And, Or, eXor, shift Left, shift Right, +, -) :\n");
    	scanf(" %c", &c);
    
    	switch (c)
    	{
    		case 'a':									//and
    			and = a & b;
    			printf("Hexadecimal result: %x %c %x = %x\n", 'a','c','b','and');
    			printf("Decimal result: %d %c %d = %d\n", 'a','c','b','and');
    			break;
    		case 'o':									//or
    			or = a | b;
    			printf("Hexadecimal result: %x %c %x = %x\n", 'a','c','b','or');
    			printf("Decimal result: %d %c %d = %d\n", 'a','c','b','or');
    			break;
    		case 'x':									//exor
    			exor = a ^ b;
    			printf("Hexadecimal result: %x %c %x = %x", 'a','c','b','exor');
    			printf("Decimal result: %d %c %d = %d\n", 'a','c','b','exor');
    			break;
    		case 'l':									//shift left
    			shiftleft = a << b;
    			printf("Hexadecimal result: %x %c %x = %x\n", 'a','c','b','shiftleft');
    			printf("Decimal result: %d %c %d = %d\n", 'a','c','b','shiftleft');
    			break;
    		case 'r':									//shift right
    			shiftright = a >> b;
    			printf("Hexadecimal result: %x %c %x = %x\n", 'a','c','b','shiftright');
    			printf("Decimal result: %d %c %d = %d\n", 'a','c','b','shiftright');
    			break;
    		case '+':									//+
    			add = a + b;
    			printf("Hexadecimal result: %x %c %x = %x\n", 'a','c','b','add');
    			printf("Decimal result: %d %c %d = %d\n", 'a','c','b','add');
    			break;
    		case '-':									//-
    			subtract = a - b;
    			printf("Hexadecimal result: %x %c %x = %x\n", 'a','c','b','subtract');
    			printf("Decimal result: %d %c %d = %d\n", 'a','c','b','subtract');
    			break;
    		default:
    			printf("illegal operation code\n");
    			return (0);
    			break;
    	}
    
    	
    }
    edit
    uhmmm no im wrong it doesnt need those ' '
    Last edited by Tom.b; 09-18-2009 at 03:24 PM.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    I don't know what MK is complaining about.
    OMG! I do. For some reason I totally missed this stuff:
    Code:
    and = a & b;
    Thus I the whole thing seemed really silly. Sorry sorry sorry. Obviously I have been staring at the screen for too long, or something.

    Just ignore me from now on Tom. Hari kari time for MK27. Horrific. Absolute shame. To make up for it I'll compile this and see if there is anything constructive I can contribute
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You had it right without the quotes.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, the only problem is all the single quotes you added. And:

    warning: control reaches end of non-void function

    So here's the 'corrected' code...see my comment at the end for the real solution to problem #1:
    Code:
    #include <stdio.h>
    
    int main ()
    {
    	unsigned int a;
    	unsigned int b;
    	char c;
    	unsigned int and;
    	unsigned int or;
    	unsigned int exor;
    	unsigned int shiftleft;
    	unsigned int shiftright;
    	unsigned int add;
    	unsigned int subtract;
    
    
    	printf("32-bit ALU simulator by Tom Bruls.\n\n");
    	printf("Please enter the values of operands a and b in hex :\n");
    	scanf(" %x %x", &a, &b);
    
    	printf("Enter operation code (And, Or, eXor, shift Left, shift Right, +, -) :\n");
    	scanf(" %c", &c);
    
    	switch (c)
    	{
    		case 'a':									//and
    			and = a & b;
    			printf("Hexadecimal result: %x %c %x = %x\n", a,c,b,and);
    			printf("Decimal result: %d %c %d = %d\n", a,c,b,and);
    			break;
    		case 'o':									//or
    			or = a | b;
    			printf("Hexadecimal result: %x %c %x = %x\n", a,c,b,or);
    			printf("Decimal result: %d %c %d = %d\n", a,c,b,or);
    			break;
    		case 'x':									//exor
    			exor = a ^ b;
    			printf("Hexadecimal result: %x %c %x = %x", a,c,b,exor);
    			printf("Decimal result: %d %c %d = %d\n", a,c,b,exor);
    			break;
    		case 'l':									//shift left
    			shiftleft = a << b;
    			printf("Hexadecimal result: %x %c %x = %x\n", a,c,b,shiftleft);
    			printf("Decimal result: %d %c %d = %d\n", a,c,b,shiftleft);
    			break;
    		case 'r':									//shift right
    			shiftright = a >> b;
    			printf("Hexadecimal result: %x %c %x = %x\n", a,c,b,shiftright);
    			printf("Decimal result: %d %c %d = %d\n", a,c,b,shiftright);
    			break;
    		case '+':									//+
    			add = a + b;
    			printf("Hexadecimal result: %x %c %x = %x\n", a,c,b,add);
    			printf("Decimal result: %d %c %d = %d\n", a,c,b,add);
    			break;
    		case '-':									//-
    			subtract = a - b;
    			printf("Hexadecimal result: %x %c %x = %x\n", a,c,b,subtract);
    			printf("Decimal result: %d %c %d = %d\n", a,c,b,subtract);
    			break;
    		default:
    			printf("illegal operation code\n");
    			return (0);
    			break;
    	}
    
    	return 0;	
    }
    Your earlier problem, vis, "why 101010", well, it is the same as 0101010, with no leading zero. If you and something with ffffff, presuming the numbers require the same number of bytes, then you will just get the same number back, since ffffff is all bits set. Make sense?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    Sep 2009
    Location
    Netherlands
    Posts
    17
    Yes that makes sense. But this doesnt:
    http://i38.tinypic.com/b66f6u.jpg
    Why do i get the -1 value?

    I use Visual C++ 2008 Express Edition. No warnings or errors here.
    Last edited by Tom.b; 09-18-2009 at 03:39 PM.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's what 0xffffffff is, -1. Or did you want to print it as though it was an unsigned int? You should have read your man printf.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    That's what 0xffffffff is, -1. Or did you want to print it as though it was an unsigned int?
    Ie., %u.

    The first bit of a (signed) integer is the sign, 0 = positive, 1 = negative.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM