"expected primary-expression before "else" ???

This is a discussion on "expected primary-expression before "else" ??? within the C++ Programming forums, part of the General Programming Boards category; im working on a program used for storing info (email, passcodes). it would keep the info safe useing "If" (code ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    3

    "expected primary-expression before "else" ???

    im working on a program used for storing info (email, passcodes). it would keep the info safe useing "If" (code != 1098765432) display "Denied" then ues a nother "if" for comands to breing up surtan lists (the code explanes it...). im new to programing and im haveing some problems, the main is that on line 21. it says 21.
    "expected primary-expression before "else" "
    being new to C++ i donot know what to do. plz help, hears the code and you can copy it for your use if you want, just if you could tell me the problem and how to fix it.

    P.S. if you see eany thing else wrong, plz tell me.

    im useing Dev-C++, if its eany help, thanks!

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    Just copy paste the portion of the code where the error is
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    3
    oooo my bad the code right, hear line 21.

    #include <iostream>

    using namespace std;

    int main()
    {
    int pass;

    cout<<"Please Input Correct Pass Code: ";
    cin>> pass;
    cin.ignore();
    if ( pass != 1098765432 ) {
    cout<<"DENIED";
    }

    else {
    cout<<"DENIED";
    }
    cin.get();

    else if ( pass == 1098765432 )
    {
    cout<<"Press Enter To Continue";
    }
    cin.get();
    {
    cout<<"Data Storage, press 1 for email addresses, 2 for numbers, 3 for Pass Codes";
    cin.get();

    int Command;

    if ( Command < 2 ) {
    cout<<"";
    }
    else if ( Command == 2 ) {
    cout<<"";
    }
    else if ( Command > 2 ) {
    cout<<"";
    }
    cin.get();
    }
    }

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Use tags -

    if - else if - else statements go like this

    Code:
    if ( something )
    {
     // do this
    }
    else if ( something else )
    {
      // do this instead
    }
    else if ...
    {
      // do other things
    }
    else 
    {
      // last possibility. 
    }
    they can't go

    Code:
    if ( something )
    {
      // do this
    }
    else 
    {
      // blah
    }
    cin.get(); <- ERROR HERE *
    else if ( blah )
    {
    // error
    }
    * this errors it 'cause there can't be anything between the if else statements.

    Also, if you have an if - else if - else - else if (I don't know, you may get a warning), the last else if won't ever occur. I suspece you should change the else if in your code, the error line to a plane old if

    EDIT - You're new, so to use CODE TAGS, select your code, and click the button that says 'CODE TAGS'.
    Last edited by twomers; 08-05-2006 at 07:21 PM.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    First of all, use code tags (see the sticky on this board). Now, think about your logic here:
    Code:
    if ( pass != 1098765432 ) { 
    cout<<"DENIED"; 
    }
    
    else {
    cout<<"DENIED"; 
    }
    Here you check if the pass is incorrect. If so, you output "DENIED". If not (else) you output "DENIED". That's obviously not what you want.

    And listen to what twomers said.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I was going to wait to see if he noticed. The best thing to do, I think, is to check if the password is right, then deal with that happy instance. THen have an else to deal with the hackers or whatever.
    >> And listen to what twomers said. - thanks
    I'd do something more like:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main( void )
    {
    	string pass;
    
    	cout<< "Enter pass: ";
    	cin >> pass;
    
    	if ( pass == "42" )
    	{
    		// do whatever
    	}
    	else
    	{
    		cout<< "DENIED!";
    	}
    
    	return 0;
    }
    I changed the int pass to a string, against the possibility that the variable (student number?), would be too large a number for an integer variable. Possibly needed, possibly not.
    Last edited by twomers; 08-05-2006 at 07:30 PM.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    3
    thank you all, i can go on saying how helpfull this is!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected primary expression
    By lilhawk2892 in forum C++ Programming
    Replies: 10
    Last Post: 11-22-2007, 06:50 PM
  2. expected primary expression before "." token
    By melodious in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2007, 04:39 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 08:15 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21