C++ errors help

This is a discussion on C++ errors help within the C++ Programming forums, part of the General Programming Boards category; the code is this: Code: #include <stdio.h> #include <math.h> int main () { int a, b, r1, r2; printf("type in ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    somewhere in terran
    Posts
    12

    C++ errors help

    the code is this:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main ()
    {
        int a, b, r1, r2;
        
        printf("type in the 2 roots:");
        scanf("%d%d" &a, &b);
        r1 = a+b;
        r2 = a*b;
        printf("the equation in general form is: x*x+(%f)x+(%f)"r1, r2;
        system ("pause");
        return 0;
    }
    why doesn't it work?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because you don't have a comma between your close quotation and r1, or between the close quote and &a.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    somewhere in terran
    Posts
    12
    thanks
    i smash my head in the table when hearing this

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    somewhere in terran
    Posts
    12
    another new problem occurs.....
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main ()
    {
        int a, b, r1, r2;
        
        printf("type in the 2 roots:");
        scanf("%d%d", &a, &b);
        r1 = a+b;
        r2 = a*b;
        printf("the equation in general form is: x*x+(%f)x+(%f)", r1, r2);
        
         system("pause");
         return 0;
    }
    the program says that there is error in those red words i labelled
    how come there is a mistake?

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    Probably you missed the header for the system function.

    It always helps to post exactly what your compiler was complaining about, so people don't have to copy/paste your code into their compiler again, to get the same message you already knew about.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    You should not be using scanf in the first place, or printf, unless you are actually doing C instead of C++ (whose section you have posted in)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    somewhere in terran
    Posts
    12
    it's C++

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    First of all compiling your this code with g++ doesn't mean it c++ what features of c++ you are using in it...

    well the errors are

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <windows.h> // include this file on ms windows for system API call
    
    int main ()
    {
        int a, b, r1, r2;
        
        printf("type in the 2 roots:");
        scanf_s("%d %d", &a, &b); // , coma was missing
        r1 = a+b;
        r2 = a*b;
        printf("the equation in general form is: x*x+(%f)x+(%f)",r1, r2); // ) brace and , was missing
        system ("pause");
        return 0;
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    Actually, <cstdlib> is the correct header for std::system. Or if you prefer to use the C header, it would be <stdlib.h>.

    By the way, please do not change the code to use the non-standard scanf_s, at least not without explaining very clearly the ramifications of such a change.
    Last edited by laserlight; 10-23-2009 at 02:39 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by laserlight View Post
    Actually, <cstdlib> is the correct header for std::system. Or if you prefer to use the C header, it would be <stdlib.h>.

    By the way, please do not change the code to use the non-standard scanf_s, at least not without explaining very clearly the ramifications of such a change.

    About header file u are correct that can be done also



    And about scanf_s on microsoft see this

    warning C4996: 'scanf' was declared deprecated
    C:\Program Files\Microsoft Visual Studio 8\VC\include\stdio.h(295) : see declaration of 'scanf'
    Message: 'This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    Quote Originally Posted by RockyMarrone
    And about scanf_s on microsoft see this
    It would be more helpful to mkmk007 if you actually linked to the MSDN article.

    But my point is that you need to explain, not just make the change. Yes, when used incorrectly scanf is unsafe, but here its usage is fine, other than being a little odd in a C++ program. However, even if the usage of scanf was insecure, by making that change, you can cause the program to fail to compile, because scanf_s is not guaranteed to be present. This is an important ramification to consider. It should also be considered that Microsoft's _s versions of various standard library functions are more of a safety net than an actual improvement in security since they detect problems at run time rather than pre-empt them.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Oct 2009
    Location
    somewhere in terran
    Posts
    12
    Thanks guys.....
    the problem is solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Unknown Errors in simple program
    By neandrake in forum C++ Programming
    Replies: 16
    Last Post: 04-06-2004, 02:57 PM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 05:51 PM

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