Thread: Newbie c programmer need assistance plz...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    26

    Newbie c programmer need assistance plz...

    Right guys and gals, i have been trying to create an registration program and im stuck at the first hurdle, i keep getting an error that i dont know how to solve. There seems to be parse error before the 'else if' and i can't figure out whats wrong.
    It's driving me nuts!


    Code:
    #include <stdio.h>
    #include <math.h>
    
    int user;
    
    int main (void)
    
    
    {
    
    	int log;
    	int reg;
    	int ans;
    	
    	printf ("Press [L]ogin or [R]egistration");
    	scanf  ("%s", ans);
    	
    	{	 
    		if (ans=='l') or (ans=='L');
     	   		Login();
    	
    		else if (ans=='r') or (ans=='R')
    			Registration();
    	
    		else 
    			printf("Invalid!\n");
    	}
    		
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by Nexus-ZERO View Post
    [B]
    Code:
    	
    	{	 
    		if (ans=='l') or (ans=='L'); <======== get rid of this ;
     	   		Login();
    	
    		else if (ans=='r') or (ans=='R')
    			Registration();
    	
    		else 
    			printf("Invalid!\n");
    	}
    		
    }
    See comment above

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    Sorry mate that hasnt solved it.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Try enclosing the conditional statement inside brackets, like : Do it for all your conditions.

    if( )
    else if ( )
    else ( )

    example:

    if( ans == 'l' or ans=='L' )

  5. #5
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Also, you have defined 'ans' as an 'int', yet you scan for a 'string'. Since you are only asking for a single character, define 'ans' as a 'char'. Your 'if' statement is testing for a 'char', not a string!

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by kcpilot View Post
    Also, you have defined 'ans' as an 'int', yet you scan for a 'string'. Since you are only asking for a single character, define 'ans' as a 'char'. Your 'if' statement is testing for a 'char', not a string!
    I missed that.

    To Orig Poster, also change your scanf( "%s", ans) to scanf( "%c", ans);

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    Ok done what was said, but it gave me even more problems

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code and how does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Post what your code looks like at this point. Have you actually created the Login and Registration functions you're attempting to call? Saying "gave me even more problems" without any additional relevant detail is fairly useless to anyone trying to help.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    Sorry about that! I've just sorted most of the errors now i only have one. This what i have done so far.
    It seems to be having a problem with my if statement.

    ERRORS
    Line 20: parse error before 'Login'
    Line 22: parse error before 'else'

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int user;
    
    int main (void)
    
    
    {
    
    	int log;
    	int reg;
    	unsigned char ans;
    	
    	printf ("Press [L]ogin or [R]egistration");
    	scanf  ("&#37;c", ans);
    	
    	{	 
    		f (ans=='l') or (ans=='L) 
    20:		     Login();
    		
    22:		else if (ans=='r') or (ans=='R')
    			Registration();
    	
    		else 
    			printf("Invalid!\n");
    	}
    		
    }
    
    Registration()
    {
    	printf("");
    
    }
    
    
    
    
    Login()
    {
    	
    	printf ("Enter Login name:");
    	scanf  ("%s", user);
    
    }
    Last edited by Nexus-ZERO; 12-26-2007 at 11:13 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The first problem I see is that:
    Code:
    f (ans=='l') or (ans=='L)
    should be:
    Code:
    if (ans == 'l' || ans == 'L')
    The same goes for:
    Code:
    else if (ans=='r') or (ans=='R')
    You also need to provide function prototypes for Login() and Registration().

    Additionally, the user variable should be declared in main() instead of being global. Pass it to the functions as needed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    Sorry to keep bothering you guys but i cant get rid of this error. On line 22 i keep getting a parse error before "int", i dont understand what im doing wrong.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int Registration();
    void Login(int); 
     
    main (int a)
    {
    	int user;	
    	int log;
    	int reg;
    	char ans;
    	
    	
    	printf ("Press [L]ogin or [R]egistration");
    	
    	scanf  ("%c", ans);
    	
    	{	 	 
    		if (ans == 'l' || ans == 'L')
    :22:			Login(int)
    			
    		else if (ans == 'r' || ans == 'R')
    			Regis();
    	
    		else 
    			printf("Invalid!\n");
    	}
    }
    		
    
    
    Registration()
    {
    	printf("");
    
    }
    
    
    
    
    void Login(int b)
    {
    	int user;
    	
    	printf ("Enter Login name:");
    	scanf  ("%c", user);
    
    }

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In that context,
    Code:
    Login(int)
    does not make sense. You would pass in an int variable or literal such as:
    Code:
    Login(user);
    Of course, this still does not work, though it may compile. Assuming you really want integer login names, you might write Login() as:
    Code:
    void Login(int* user)
    {
        printf("Enter Login name:");
        scanf("&#37;d", &user);
    }
    Then use it as:
    Code:
    if (ans == 'l' || ans == 'L')
        Login(&user);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    Ok i did that, but it came up with more errors.

    On line 6: previous declaration of 'Login'
    On line 22: warning passing arg 1 of 'Login' make interger from a pointer without a cast
    On line 44: conflicting types for login

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int Registration();
    :6:void Login(int); 
     
    main (int a)
    {
    	int user;	
    	int log;
    	int reg;
    	char ans;
    	
    	
    	printf ("Press [L]ogin or [R]egistration");
    	
    	scanf  ("&#37;c", ans);
    	
    	{	 	 
    		if (ans == 'l' || ans == 'L')
    :22:		Login(&user);
    			
    		else if (ans == 'r' || ans == 'R')
    			Registration();
    	
    		else 
    			printf("Invalid!\n");
    	}
    }
    		
    
    
    Registration()
    {
    	printf("");
    
    }
    
    
    
    
    void Login(int* user)
    :44:{
    	printf ("Enter Login name:");
    	scanf  ("%d", &user);
    
    }

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Look at line 6, your Login function prototype. It doesn't match with your Login function at line 43. The prototype takes an int as a parameter, but the actual function is taking a int*. The prototype and the function need to match.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie programmer with his first frustration
    By galactic_ronin in forum C++ Programming
    Replies: 14
    Last Post: 02-20-2008, 02:39 PM
  2. Newbie needs help plz
    By boffinson in forum C++ Programming
    Replies: 22
    Last Post: 07-29-2006, 03:25 AM
  3. im newbie to opengl, help plz
    By NecroFromHell in forum C++ Programming
    Replies: 4
    Last Post: 05-14-2006, 08:24 PM
  4. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  5. HI .. newbie to the board... need some help plz
    By Arwen27 in forum C Programming
    Replies: 3
    Last Post: 04-28-2002, 07:27 AM