This forum is in archive mode. You will not be able to post new content.

Author Topic: C++ exporting as a commandline program???  (Read 4799 times)

0 Members and 2 Guests are viewing this topic.

Offline Lionofgod

  • Knight
  • **
  • Posts: 164
  • Cookies: 6
    • View Profile
C++ exporting as a commandline program???
« on: July 18, 2011, 06:52:23 PM »
Hi, so I've been learning c++, im still a nub and im only writing really basic code.
So i wrote some code,
Code: [Select]
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std ;
    int main() {
int i= 2  ;
double n;
double s ;
int x ;
int prime= true ;
cout << " Please enter a number to find out whether it is prime" << endl ;
cin >> n ;
x=n ;
s=sqrt(n) ;

    for (i=2; i<= s ; i++) {
        if (x%i==0){
            cout << " The number " << x << " is divisible by " << i << endl ;
            prime= false;}

    if (prime ) {
        cout << "The number " << x << "is a prime number" << endl ;
    }
    }
And now i want to know if their is a way i can turn this into a sort of application.
Just a command line thing, where i can run the program without microsoft visual studio.
I'd like to be able to open up the program, whenever and just run it (just for the fun of it)
Can someone please explain how to do this, cuz i googled everywhere and couldnt find shit : (
Also, i am using msvs ultimate (torrented :p), so could anyone please tell me how to do this????

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: C++ exporting as a commandline program???
« Reply #1 on: July 18, 2011, 07:02:50 PM »
Build the file, locate in bin/release folder. Drag or locate with CMD, execute from CMD.
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline Lionofgod

  • Knight
  • **
  • Posts: 164
  • Cookies: 6
    • View Profile
Re: C++ exporting as a commandline program???
« Reply #2 on: July 18, 2011, 07:10:42 PM »
Build the file, locate in bin/release folder. Drag or locate with CMD, execute from CMD.
I lost you at, locate in bin/release folder, do you mean the source file?
But the source file is located in the project folder of vs2010. ???

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: C++ exporting as a commandline program???
« Reply #3 on: July 18, 2011, 07:18:10 PM »
I lost you at, locate in bin/release folder, do you mean the source file?
But the source file is located in the project folder of vs2010. ???

When you make a new project, Visual Studio creates a project folder for you. Its called whatever you name your project. Inside that, there should be a bin directory. And inside that again, there should be a debug and a release dir. The debug directory will contain builds of your debug version. The release dir will contain the final built stand-alone executable that can be run without any debugging. Note that you can run the debug version as well, but it is slightly modified to be easier to debug then the release version.
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline Lionofgod

  • Knight
  • **
  • Posts: 164
  • Cookies: 6
    • View Profile
Re: C++ exporting as a commandline program???
« Reply #4 on: July 18, 2011, 07:39:41 PM »
Okay, their is no release directory, but i did find a Debug directory.Like you said, it has a .exe version of my program, problem is, after i input a number, the program just exits/dissapears. I tried adding some code at the end of the program, which would require  the user to confirm an exit, but that didn't stop the program from dissapearing!
like i said before their is absolutely no release directory. I checked in every folder, sub-folder..etc
« Last Edit: July 18, 2011, 07:45:59 PM by Lionofgod »

Offline Lionofgod

  • Knight
  • **
  • Posts: 164
  • Cookies: 6
    • View Profile
Re: C++ exporting as a commandline program???
« Reply #5 on: July 18, 2011, 07:59:17 PM »
Okay my bad i figured it out.
I added
Code: [Select]
system("PAUSE");
return 0;
at the end and now it works perfectly.
Thanks a lot for the help :D

Offline petermlm

  • Knight
  • **
  • Posts: 226
  • Cookies: 7
  • Information is Power
    • View Profile
    • Security Check
Re: C++ exporting as a commandline program???
« Reply #6 on: July 19, 2011, 12:02:41 AM »
Okay my bad i figured it out.
I added
Code: [Select]
system("PAUSE");
return 0;
at the end and now it works perfectly.
Thanks a lot for the help :D

Hello. You should have a look to the function getchar(). That function sometimes has to be written twice if you have a function like scanf() in your code, but then again, I advice you to have a look at it.

Offline Lionofgod

  • Knight
  • **
  • Posts: 164
  • Cookies: 6
    • View Profile
Re: C++ exporting as a commandline program???
« Reply #7 on: July 19, 2011, 04:00:36 AM »
Hello. You should have a look to the function getchar(). That function sometimes has to be written twice if you have a function like scanf() in your code, but then again, I advice you to have a look at it.
hey, i found an example online, i partially understood it, so i tried it out.
Code: [Select]
// new.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdio.h>

 void print(int a, int b) ;
 void enter(void) ;
using namespace std;

 int main()
{
    int x;
    int y;
    cout << "Enter the first number" << endl;
    cin >> x ;
    cout << "Enter the second number" << endl;
    cin >>y;
    while (x>=y){
        cout << "The first number must be smaller then the second! Please enter the first number again!" << endl ;
        cin >>x ;
        cout << "Now enter the second number, again " << endl;
        cin>>y;}
        print(x,y) ;
        cout << enter;
}
void print(int a, int b) {
    while ( a<=b){
   
    cout << a << endl;
    a++ ;
    }

}

    void enter(void)
         { printf("Press ENTER to continue: ");
           char c=getchar();
           while (c != '\n')
             c=getchar(); }
Unfortunately, it didnt work, the application exited right after i inputted the first two numbers, when using the system("PAUSE") method, it works. Maybe i am using getchar() in the wrong way, would you care to explain?


Offline petermlm

  • Knight
  • **
  • Posts: 226
  • Cookies: 7
  • Information is Power
    • View Profile
    • Security Check
Re: C++ exporting as a commandline program???
« Reply #8 on: July 19, 2011, 12:02:04 PM »
You are not using the getchar() wrong. When something is written in the terminal for an input function like cin, scanf or getchar, the ENTER stays in the buffer, then, when another input function gets called that enter in the buffer just in and passes the function.

If you write something like:

Code: [Select]
cin >> x;
getchar();

When this code gets executed the ENTER you press in the line "cin >> x" only gets read in the function getchar(), so the function seams to get ignored.

See what I mean?

To go around this you have several options, they are:

Code: [Select]
cin >> x;
getchar();

//more code

getchar(); //At the end of the program

or

Code: [Select]
cin >> x;
fflush(stdin);

//more code
 
 getchar(); //At the end of the program

or

Code: [Select]
cin >> x;

//more code
 
 getchar(); //Double getchar at the end of the program


Among others.

I do not recomend you using system("pause") because that only work's in windows. If you were to compile this program in a Linux or Mac environment, because pause is not a valid command for those terminals, this would not work.

Offline Lionofgod

  • Knight
  • **
  • Posts: 164
  • Cookies: 6
    • View Profile
Re: C++ exporting as a commandline program???
« Reply #9 on: July 19, 2011, 09:19:47 PM »
Alright thanks that makes sense and it works too :D
Thanks bro : )

Offline petermlm

  • Knight
  • **
  • Posts: 226
  • Cookies: 7
  • Information is Power
    • View Profile
    • Security Check
Re: C++ exporting as a commandline program???
« Reply #10 on: July 20, 2011, 12:23:20 AM »
Alright thanks that makes sense and it works too :D
Thanks bro : )

You are welcome. Happy Hacking!!

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.