Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I run C++ code in Qt? [solved]
Forum Updated to NodeBB v4.3 + New Features

How do I run C++ code in Qt? [solved]

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 4.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SweetOrange
    wrote on 26 Feb 2014, 12:18 last edited by
    #1

    Hi I have following code:

    @#include <QCoreApplication>

    using namespace std;

    unsigned long factfunc(unsigned long);

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    int n;
    unsigned long fact;
    
    cout << "Enter an integer: ";
    cin >> n;
    
    fact = factfunc(n);
    
    cout << "Factorial of " << n << " is " << fact << endl;
    
    return a.exec&#40;&#41;;
    

    }

    unsigned long factfunc(unsigned long n)
    {
    if(n>1)
    return n * factfunc(n-1);
    else
    return 1;
    }
    @

    Errors: cout & cin was not declared in this scope

    Probably I cannot use namespace std here. Do I need to do something else to use
    C++ code in Qt? I will be writing a lot of C++ code here so please help.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mikeosoft
      wrote on 26 Feb 2014, 12:23 last edited by
      #2

      Are your including the headers, I think below should do the trick?

      @#include <iostream>@

      1 Reply Last reply
      0
      • A Offline
        A Offline
        ankursaxena
        wrote on 26 Feb 2014, 12:45 last edited by
        #3

        u have to use std::cout and std::cin in spite of cout and cin and please include <stdio.h> and <iostream.h>

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mikeosoft
          wrote on 26 Feb 2014, 13:18 last edited by
          #4

          You would use the std:: element if you weren't using the namespace, so it should work without that. I know some don't like using namespaces and would only use the above approach.

          The stdio.h header file is a C rather than C++ header file, so it shouldn't effect the standard library use.

          The iostream header file should be used without the .h as that signfies you are using an older version, the newer standard libaries use headers without the extensions.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SweetOrange
            wrote on 26 Feb 2014, 20:18 last edited by
            #5

            Yes that definitely did the trick. The following code runs ok. Thanks mlkeosoft

            @#include <QCoreApplication>
            #include <iostream>

            using namespace std;

            unsigned long factfunc(unsigned long);

            int main(int argc, char *argv[])
            {
            QCoreApplication a(argc, argv);

            int n;
            unsigned long fact;
            
            cout << "Enter an integer: ";
            cin >> n;
            
            fact = factfunc(n);
            
            cout << "Factorial of " << n << " is " << fact << endl;
            
            return a.exec(&#41;;
            

            }

            unsigned long factfunc(unsigned long n)
            {
            if(n>1)
            return n * factfunc(n-1);
            else
            return 1;
            }
            @

            How do I mark this post as solved?

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dvb0222
              wrote on 26 Feb 2014, 23:58 last edited by
              #6

              To mark it as solved just edit your original post and prepend or append "[solved]" to the title.


              David Van Beveren
              Oak Park Technology Corp.
              Malibu, California
              vbdavid@gmail.com

              1 Reply Last reply
              0
              • C Offline
                C Offline
                CroCo
                wrote on 27 Feb 2014, 03:56 last edited by
                #7

                [quote author="ankursaxena" date="1393418751"]u have to use std::cout and std::cin in spite of cout and cin and please include <stdio.h> and <iostream.h>[/quote]

                Why does he need to include "<stdio.h>"? And why he has to use std::cout since he included "usine namespace std;"

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  ankursaxena
                  wrote on 27 Feb 2014, 04:08 last edited by
                  #8

                  thanx mikeosoft! you are right. He just need <iostream> .

                  1 Reply Last reply
                  0

                  4/8

                  26 Feb 2014, 13:18

                  • Login

                  • Login or register to search.
                  4 out of 8
                  • First post
                    4/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved