Expected primary expression before ';',"int" comes because...?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yoyo22
    New Member
    • Dec 2012
    • 2

    Expected primary expression before ';',"int" comes because...?

    Code:
    #include<cstdio>
    #include<cstdiolib>
    #include<iostream>
    using namespace std;
    int main(int nNumberofArgs, char* pszArgs[])
     
    {
        cout<<"Enter your account type";
        int FD;
        if ("FD")
        {
             int p=10000000;
             float r=10;
             int t;
             cin>>t;         
             cout<<p*r*t/100;
             
        }
        int RD;
        else ("RD")[CODE]
    {
    int p=1000000;
    float r=10;
    int t;
    cin>>t;
    cout<<p*r*t/100;
    }
    system("PAUSE") ;
    return 0;
    }[/code]
    Last edited by Rabbit; Dec 17 '12, 04:56 PM. Reason: Please use code tags when posting code.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    It's probably right here:

    Code:
     cout<<int si=p*r*t/100;
    You can't create a variable this way. You calling an operator<< function. This one has an ostream& on the left and some argument on the right. You will need to review which types are available as the right hand argument.

    If all you want to see is p*r*t/100 then use that and remove the int si=.

    BTW: Please use CODE tags on future posts.

    Comment

    • yoyo22
      New Member
      • Dec 2012
      • 2

      #3
      Thank you. I will use the code tags in my future posts.

      Comment

      • divideby0
        New Member
        • May 2012
        • 131

        #4
        this doesn't look like either; I'd think it would lead to illegal else without if or such.

        what is being tested if("FD")? I've not seen that before.

        Code:
        if("FD") {
           ...
        } 
        
        int RD;
        
        else("RD") {
           ...
        }
        if the result of p * r * t / 100 isn't what you'd expect, use parenthesis around the operators.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          "FD" is a string literal. What is being tested is the address of te literal. This will always be true since the literal exists.

          I expect "FD" is a bug since there is an int named FD and probably should have ben coded:

          Code:
          if (FD)
          {
          etc...
          In this case you are testing the contents of the variable FD to be zero or not. If zero, the if will be false.

          On the other hand, nothing is ever put into FD so the whole program makes no sense.

          Comment

          • divideby0
            New Member
            • May 2012
            • 131

            #6
            Thank you.

            I couldn't figure out what if("FD") was supposed to be; is that ever useful? The int was more of what I'd expect... that is, testing a declared type.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              It may be useful but it's bad practice to have hard-coded values in your code. A better choice is to use a singleton object. The object is created before the program starts and stays around until the program completes. The creation process could read a disc file to get the "FD". That way you could change the value by changing the disc file rather than recompiling the code and sending copies to everyone that has the program.

              Code:
              Singleton obj;  //reads disc file
              int main()
              {
                 string data;
              etc.....
              
                 if (data == obj.data())
                 {
                    //data equal to the disc file value here
                 }
              etc...
              }
              As you see, the code is now independent of the value "FD".

              A not as good approach is to use a macro. Here you define the macro as "FD". This approach keeps the "FD" in one location but to change it you need to recompile the code:

              Code:
              #define  VALUE  "FD"
              This is put in a header file, say macro.h.

              Then you:

              Code:
              #include <macro.h>
              
              int main()
              {
                 string data;
              etc.....
              
                 if (data == VALUE)
                 {
                    //data equal to the macro definition here
                 }
              etc...

              Comment

              • johny10151981
                Top Contributor
                • Jan 2010
                • 1059

                #8
                Interestingly the grammatical error you have made is
                Code:
                 if(condition)
                 {
                  statements;
                 }
                  statements; // this is the line where you have made your grammatical error
                 else
                 {
                  statements;
                 }
                in your original code line 19

                Now first you ask account type:
                but you didn't write any statement to read the account type

                instruction:
                Code:
                char account_type[4]
                cin>>account_type;
                then you defined integer type FD. FD is a variable without any assigned value.

                You don't need FD or RD

                What you need is in if condition compare whether the account is FD or RD.
                Instruction:
                Code:
                if(strcomp(account_type,"FD")==0)
                {
                  Statements;//
                }
                else if(strcomp(account_type,"RD")==0)
                {
                 Statements;
                }
                else 
                {
                 cout<<"Invalid Account Type Inputted"<<endl;
                }

                Comment

                Working...