LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-17-2010, 05:53 AM   #1
iqra
Member
 
Registered: May 2010
Posts: 34

Rep: Reputation: 15
finding error


hiii...

i running a program of C heres one function in which i am getting an error.
Code:
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....!!!
 
Old 06-17-2010, 06:03 AM   #2
Gavin Harper
Member
 
Registered: Feb 2010
Location: Finland
Distribution: Slackware
Posts: 169

Rep: Reputation: 24
The ' ' around the 1 and 2 in the switch imply that the switch ases are looking for items of type char which is why it isn't matching to the switch cases.

Remove the ' ' and it will recognise switch_val as an integer
 
Old 06-17-2010, 06:24 AM   #3
iqra
Member
 
Registered: May 2010
Posts: 34

Original Poster
Rep: Reputation: 15
switch_val is an integer
and i tried by removing ' ' it is giving segmentation fault error.

is it becasue of any problem in my get() and getnext() functions?
either choosing case 1 or case 2.... same error occurs..

if i take switch_val as a char then it keeps on going in default case
 
Old 06-17-2010, 07:38 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

try the following (changes in bold and red)

Code:
void snmp()
{
int count;
int 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;
    }

}

Last edited by crts; 06-17-2010 at 07:42 AM. Reason: colors
 
1 members found this post helpful.
Old 06-17-2010, 08:00 AM   #5
iqra
Member
 
Registered: May 2010
Posts: 34

Original Poster
Rep: Reputation: 15
@ crts
thnx

i just tried this and it worked fine...
anyways thanx for help..
 
Old 06-17-2010, 09:16 AM   #6
Gavin Harper
Member
 
Registered: Feb 2010
Location: Finland
Distribution: Slackware
Posts: 169

Rep: Reputation: 24
I totally missed that switch_val was declared as a char
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GRUB error finding root filesystem partition smanandhar Linux - Newbie 3 02-16-2009 10:47 AM
compilation error: something to do with finding the right kernel source directories? Gatemaze Linux - Kernel 4 06-15-2008 05:17 PM
finding grub error 17 with dual boot intentions kylenewton Linux - General 5 12-31-2005 07:04 PM
error when finding the standard deviation of a vector mshinska Programming 5 10-25-2005 11:03 PM
Error compiling gimp-print -- not finding libssl joe_huddleston Linux - Software 2 04-02-2004 05:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration