-3

I wrote this code to calculate the score of a student on the Greek entrance exams. When the program calculates the score and saves it in the variable moria, I want the number to appear in a pop-up window.

#include <iostream>
#include <windows.h>
using namespace std;
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)

The last part, where the score is calculated and the message-box code comes up, is this:

mathks= mathk*0.7 + mathkp*0.3 ;
aodes= aode*0.7 + aodep*0.3 ;
fysks= fysk*0.7 + fyskp*0.3 ;
aeps= aep*0.7 + aepp*0.3 ;
ek8eshs= ek8esh*0.7 + ek8eshp*0.3 ;
mathgs= mathg*0.7 + mathgp*0.3 ;
gvp=(mathks+aodes+fysks+aeps+ek8eshs+mathgs)/6 ;
x=mathk*1.3 ;
y=fysk*0.7 ;
moria=(gvp*8+x+y)*100 ;
string moria2 = to_string(moria);
MessageBox(NULL, moria2, "arithmos moriwn", NULL);

To print the number which is a long, I think I have to turn it into a string first. But it still won't work, and I get the following errors:

  1. 'to_string' was not declared in this scope
  2. cannot convert 'std::string' to 'const CHAR*' for argument '2' to 'int MessageBoxA(HWND__, const CHAR, const CHAR*, UINT)'

Since I only recently started learning a few things about graphics, there might be some really dumb mistakes here, so please be understanding...

Wandering Fool
  • 2,170
  • 3
  • 18
  • 48

2 Answers2

0

Problem #1: converting an int to a std::string

to_string isn't declared, so the compiler doesn't know what you are trying to do. I'm going to assume moria is an int (or some number).

To convert an int to a std::string, you should have a look at this question which highlights three great ways of doing so: atoi, std::ostringstream, and most recently and also the best std::to_string (which it appears you are trying to use).

To properly use std::to_string, you should write the following:

std::string moria2 = std::to_string(moria);

Note that std::to_string is only from C++11 which I assume you are using. If you do not have C++11 prefer std::ostingstream:

std::ostringstream oss;
oss << moria;
std::string moria2 = oss.str();

As a side-note, you should prefer to name your variables indicatively, not just appending numbers.

Problem #2: converting a std::string to a const char*

Your second problem is that MessageBox takes a const char* as its second arguement, and you are providing a std::string (which the compiler cannot implicitly convert to); however, std::string gracefully provides you a way of doing so: std::string::c_str(). Your code should thus be:

MessageBox(NULL, moria2.c_str(), "arithmos moriwn", NULL);

Or

MessageBox(NULL, std::to_string(moria).c_str(), "arithmos moriwn", NULL);

It's worth noting the two commenters @πάντα ῥεῖ (my apologies if this is incorrect) and @Jonathan Potter who first pointed this out.

Community
  • 1
  • 1
Tas
  • 7,023
  • 3
  • 36
  • 51
  • The second argument to `MessageBox` is a `const TCHAR*`. If you want to pass a `const char*`, you need to call `MessageBoxA`. Ideally, though, you'd call `MessageBoxW` and use a `std::wstring`. – IInspectable Aug 02 '15 at 23:56
  • It would seem your advice sold the second error (thanks a lot for that), however when i use the function to_string i get the following errors: if i used std::to_string then it says to_string is not a member of std, and if i dont it says to_string was not declared in this scope. So i am kinda stuck here :/ – Vlassis Fotis Aug 03 '15 at 12:55
  • @VlassisFotis: As the [documentation](http://en.cppreference.com/w/cpp/string/basic_string/to_string) explains, you'll have to `#include `. – IInspectable Aug 03 '15 at 17:33
  • Well... i am not that dumb. Obviously i included it, but could i , perhaps, have an older version of it or something where the to_string is not included? – Vlassis Fotis Aug 03 '15 at 18:14
  • @VlassisFotis `std::to_string` is C++11 (see the link). If you are not compiling with C++11 I suggest you use `std::ostringstream` (there's an example in the linked question) – Tas Aug 03 '15 at 20:59
  • Thanks! But in the end i included the funtction definition itself in the code and it worked. Thanks a lot for your advice though, i will keep it in mind for my next problem. – Vlassis Fotis Aug 04 '15 at 09:45
0

It seems that you have not included <string>, therefore to_string() was not declared.

The problem 2 is already resolved by the above answer.

William Chan
  • 277
  • 2
  • 12