Announcement

Collapse
No announcement yet.

coding help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    coding help

    Alright I will probably catch shit for this but I need help with this basic problem for my C++ class. So someone who knows coding see if you can help. i keep getting the error that 'mult' : identifier not found when i try to compile the program, and if i try to identify it, it states too many identifiers. if someone could help that would be swell. here is the code.

    #include <iostream>
    using namespace std;
    int main ( ) {

    double b, e;
    int count=1;
    while (count<4)
    cout<<"Please input first number: "<<endl;
    cin>>b;
    cout<<"Please input second number: "<<endl;
    cin>>e;

    mult(b,e);
    cout<<mult<<endl;
    count++;

    system("pause");
    return 0;
    }

    double mult(double num1, double num2) //function prototype
    { return (num1*num2);

    }

    I am in no way good at programming nor have a desire to pursue this study past this semester, but i needed extra credits for my engineering degree so ya, any help is appreciated.
    sigpic

    #2
    Originally posted by Car Addict View Post
    Alright I will probably catch shit for this but I need help with this basic problem for my C++ class. So someone who knows coding see if you can help. i keep getting the error that 'mult' : identifier not found when i try to compile the program, and if i try to identify it, it states too many identifiers. if someone could help that would be swell. here is the code.

    #include <iostream>
    using namespace std;
    int main ( ) {

    double b, e;
    int count=1;
    while (count<4)
    cout<<"Please input first number: "<<endl;
    cin>>b;
    cout<<"Please input second number: "<<endl;
    cin>>e;

    mult(b,e);
    cout<<mult<<endl;
    count++;

    system("pause");
    return 0;
    }

    double mult(double num1, double num2) //function prototype
    { return (num1*num2);

    }

    I am in no way good at programming nor have a desire to pursue this study past this semester, but i needed extra credits for my engineering degree so ya, any help is appreciated.
    I haven't programmed in C++ in a while but I think I got this...

    Your error is probably in this part:
    mult(b,e);
    cout<<mult<<endl;

    The error is basically saying "mult in cout<<mult<<endl; is not defined" ... It thinks mult is a variable, you can't call a function like that with just the name and no arguments, you call it like you did in the line before. Just "mult", without (arguments) is recognized as a variable by the compiler.
    mult(b,e); isin't actually doing anything useful here. The function is executing, but you need to store the result somewhere.
    Like this:
    double x = mult(b,e);
    then for the next line:
    cout<<x<<endl;

    This might work too instead of that but i'm hazy on the syntax and what c++ allows:
    cout<<mult(b,e)<<endl;

    /CS major
    Zinno '89 <24v swap in progress>

    Comment


      #3
      ^just tried that same error as before "'mult':identifier not found".

      this is starting to piss me off.

      any other ideas?
      sigpic

      Comment


        #4
        What does your code look like now?
        And what line is it saying the error is at?
        If you're using any decent compiler it should tell you the line that the error is at.
        Zinno '89 <24v swap in progress>

        Comment


          #5
          double b, e;
          int count=1;
          while (count<4)
          cout<<"Please input first number: "<<endl;
          cin>>b;
          cout<<"Please input second number: "<<endl;
          cin>>e;

          double x= mult(b,e);
          cout<<"The product is "<<x<<endl;
          count++;

          system("pause");
          return 0;
          }
          double mult(double num1, double num2)
          { return (num1*num2);

          }

          that is what the code looks like and the blue color is the line with the error.
          sigpic

          Comment


            #6
            edit.. wrong
            Last edited by VinniE30; 11-07-2011, 08:43 PM.
            Zinno '89 <24v swap in progress>

            Comment


              #7
              This is pretty simple stuff. Are you a freshmen CS major in an intro course?
              Not to discourage you, but it gets *much* harder than this, it's definitely not for everyone, to be good you need to have a more science/math mind and way of thinking as opposed to the english/history major kind of mind.. if that makes sense to you.

              Oh and your while loop has no parentesis. You need to have:
              while{ (everything that you want to be in the loop) }
              Zinno '89 <24v swap in progress>

              Comment


                #8
                ^ the function mult() is being passed by reference to the outside of main().

                I know the compiler sees that because when i hover over the line for double x=mult(b,e); it has the function prototype above it.

                PS thanks for helping i really do appreciate it, also i am not a CS major i am an engineering major no desire to go the CS route only needed some computer courses for credit crap.
                sigpic

                Comment


                  #9
                  Yeah nevermind that's not it.
                  Zinno '89 <24v swap in progress>

                  Comment


                    #10
                    You need to have the function mult declared before main()
                    Zinno '89 <24v swap in progress>

                    Comment


                      #11
                      So this is what it should look like:


                      #include <iostream>
                      using namespace std;

                      double mult(double num1, double num2) //function prototype
                      { return (num1*num2);
                      }

                      int main ( ) {

                      double b = 0, e = 0;
                      int count=1;

                      while (count<4){
                      cout<<"Please input first number: "<<endl;
                      cin>>b;
                      cout<<"Please input second number: "<<endl;
                      cin>>e;

                      cout<<mult(b, e)<<endl;
                      count++;
                      } //end while

                      system("pause");
                      return 0;
                      } //end main
                      Zinno '89 <24v swap in progress>

                      Comment


                        #12
                        ^ You're the man it works fine with the function being placed before main().

                        Is there a way to get it to work with the function coming after main(). If not I will submit it this way some credit off is better then non working code.

                        Why do I ask my professor wants it after main() stupid yes i know but whatever.

                        As i said i really appreciate the help.
                        sigpic

                        Comment


                          #13
                          Not sure why you want to do that... What's not working? The code I posted above works fine for me.
                          In c++ all the functions you use in main have to be declared before main.
                          Zinno '89 <24v swap in progress>

                          Comment


                            #14
                            No it is working perfectly but my professor mentioned she does not want the function being declared globally.

                            But then again I may have just heard her wrong, you know more than I so if you say they must be declared prior to the main I will submit it that way.

                            Thank you so much for helping I would have been F'd in the A otherwise.
                            sigpic

                            Comment

                            Working...
                            X