-2

I wrote a very simple windows console application in c++ (in visual studio 2008) but when I send the .exe file to my friend it wont start. Btw the program works fine on my pc. Here is the program code:

#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#include "string"

void main()
{
    float a, b, c;
    printf("**SUM UP TWO NUMBERS**\n\n");
    printf("Enter first number: ");
    scanf("%f", &a);
    printf("Enter second number: ");
    scanf("%f", &b);
    c=a+b;
    printf("The result is: %f\n\n\n", c);
    system("pause");
}

P.S. I just started programming so don't judge if the program could be written better.

lil buda
  • 63
  • 1
  • 3
  • 11
  • Your friend is also running Windows? – Codie CodeMonkey Jul 15 '14 at 17:55
  • Did you send your friend the "release" version of your application and not the "debug" version? – PaulMcKenzie Jul 15 '14 at 17:56
  • May be a 32-bit vs 64-bit issue? What are the exact errors your friend got, and what's the actual OS he's using? – πάντα ῥεῖ Jul 15 '14 at 17:56
  • 3
    See [Why does my application require Visual C++ Redistributable package](http://stackoverflow.com/questions/16167305/why-does-my-application-require-visual-c-redistributable-package). – Joe Jul 15 '14 at 17:56
  • Why the downvotes? Beginner questions are welcome on SO. – Codie CodeMonkey Jul 15 '14 at 17:58
  • 1
    probably for lack of information. – DTSCode Jul 15 '14 at 17:59
  • 1
    `it wont start` I think that can be expanded on a little better. – PaulMcKenzie Jul 15 '14 at 17:59
  • `main` must return `int`. – chris Jul 15 '14 at 18:01
  • some recommendations for your program: don't use stdafx.h unless you have to. there is an option to turn it off. if you arent really using it, it makes it easier for others to help you by testing your code. secondly, i wouldn't put libraries in "" like that. what if you wanted to make a header file math.h? then when you wanted to use the actual math.h it wont work. third, for each standard c x.h header, there is a cx header. ie cstdio and cmath. i would use those in c++. fourth, why are you including math and string when you dont use anything from them? fifth, main must return an int. 1/2 – DTSCode Jul 15 '14 at 18:04
  • 2/2 sixth, why are you using c style io? C++ streams (ie cin and cout) are much safer. finally, don't use system(anything) ever. it is evil. there is always a better solution. – DTSCode Jul 15 '14 at 18:05
  • In this case, the better solution is to kill off the `system` line entirely and run it from the console *because it's a console application*. – chris Jul 15 '14 at 19:04
  • Tnx for all the advice :) I really JUST STARTED programing so I don't understand everything you wrote here. I don't know what all the libraries are for, I just put them in just in case, but I know I shouldn't have inluded math.h. The system("pause") line I put in the code because without it the program just exits when it calculates so i can't see result, and i found that line on this site. I have no idea what a header file math.h is and I don't know what c style io is. Please don't downvote because, as I said, I'm just a beginner. I tried what @NetVipeC wrote bellow, waiting for feedback... – lil buda Jul 16 '14 at 08:50
  • @PaulMcKenzie "it wont start" is all my friend told me, so I don't have more info. I didn't ask him what error he gets because I thought it's because he doesn't have vs installed. – lil buda Jul 16 '14 at 09:02
  • @bux - You must know exactly what you need to have a program run on your friend's machine. You don't need Visual Studio installed to run a program. You must create a `release` version of your application, and if necessary, include the Visual Studio `redistributable` files. – PaulMcKenzie Jul 16 '14 at 09:05

2 Answers2

2

1) Change build settings to Release, if it is currently on Debug
2) Open project properties
3) Goto C/C++ - Code Generation
4) Change Runtime Library to Multi-Threaded
5) Build

sp2danny
  • 7,488
  • 3
  • 31
  • 53
1

Probably your friend don't have libraries needed to executed.

C++ Console application can be compiled:
-dynamically: linked to dlls, ex: VC++RT
-statically: adding needed code to your exe

In VS2008 Go To: Properties of the Project -> Configuration Properties -> C\C++ -> Code Generation -> Runtime Library: changed to /MTd in debug and /MT in release and your binary would be auto contained and independent of dlls and other dependency.

Other options is install VC++ Runtime 2008 (match the version) in your friend PC (normally other software installed, but probably not this days).

Assuming that is no problems with architecture (32bits vs 64bits)

NetVipeC
  • 4,402
  • 1
  • 17
  • 19