hiii...

i running a program of C heres one function in which i am getting an error.

void snmp()
{
int count;
char switch_val;
	printf(" start sending requests\n");
	printf(" select '1' to send get request\n");
	printf(" select '2' to send get next request");
	scanf( "%d", switch_val);
	
	switch(switch_val)
	{
		case '1':
			
			count=0;
			if (count==0)
			{
			get();
			}
			else getnext();
			count++;
			break;
		case '2':
			getnext();
			break;
			
					
		default:
			printf("OOooPPppssss");
			break;
	}

}

program always go in default case. i can not figure it out why it is happeing. i have used switch statements manny times but on linux i have never worked before so could not debug the error properly.

if i use integer values for cases (for exmaple case '1')
then after selecting any option it gives 'segmentation fault' error.

plzzzz tell me whats wrong with this code....!!!

Recommended Answers

All 3 Replies

Line 8, you get an integer, presumably you input either 1 or 2.

But in your case statements you using '1' and '2', these are the characters 1 and 2 and respective have the values 49 and 50 (assuming ASCII) so your 1 and 2 input don't match.

Using the integer values is the correct approach which you have tried. The segmentation fault I assume is caused by something later in your code in the functions get() or getnext().

In case 1 since count is always initialised to 0 it will always call get(), the whole of case 1 can be reduced to a call to get() and the variable count can be discarded.

i dont see any mistake in my get and getnext functions... both these functions are doing same job so they have got same code in them. is that thing creating some problem?

ohhhhhhh error was on line 8 i didnt use '&' with switch_val
:-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.