-4

I want to creat a program which the user can insert time and prees enter and the windows shuts down after the time runs out.
So that was the code I used to create build the program :

int main()
{
    cout<<"enter time"<<endl;
    float m;
    cin>>m;
    system("shutdown -s -t m");
    return 0;
}

And when I run it and enter the time it doesn't work So I think the problem because Search Results Quotation mark :) So I need your help guys to tell me about any code or any solution to creat this program :)

Notice: I am rookie at learning C++ I mean I only began to study it since 3 weeks ago only and I learn it via tutorials from the internet

Bart van Nierop
  • 4,130
  • 2
  • 28
  • 32
  • 4
    do you think `system("shutdown -s -t m");` here `m` will be replaced by your input value.? – Rustam Oct 03 '14 at 11:27
  • 1
    Look up string formatting or interpolation. – matsjoyce Oct 03 '14 at 11:27
  • 2
    Don't learn via internet tutorials. Invest in [a good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Oct 03 '14 at 11:29
  • 1
    Why so many downvotes? OK, it's obvious for a lot of programmers, but maybe he's just learning. – Binary Brain Oct 03 '14 at 12:13
  • hi guys, thanks for replying , but i think you guys didnt read the last (notice) i only study programing for 3 weeks , i mean of course i will fail and do mistakes ! and for speaking about studying trough the internet: you guys should know that here in Egypt there is a really few C++ courses and extremely expencive and if i was able to take C++ course i would take it but i cant iam only 17 years old and interested in programing and i also acreated a calculator with GUI using (QT creator) :) i wish you guys understand me – Hamdi782 Oct 03 '14 at 19:25

1 Answers1

0

The problem is that your "m" is put in a string literal. It will be considered as part of your string which you pass to the system() function. What you want to do is adding that m into the string.

One possible way to do this with C++ strings would be:

int main()
{
   float m;
   // Makes and sets an ostringstream to add new elements at the end of the stream and expand it
   std::ostringstream ss (std::ostringstream::ate);

   cout<<"enter time"<<endl;
   cin>>m;

   // sets "shutdown -s -t " as the contents of the stream
   ss.str("shutdown -s -t ");
   // pushes the float element to the stream
   ss << m;

   // Calls the system function with a valid CString
   system(ss.str().c_str());

   return 0;
}

Also note that the system shutdown /t xxx command expects a number above 0 to be called with! The valid values are 0-315360000. Therefor unsigned int would be the correct data type to be used here.

Vinz
  • 3,030
  • 4
  • 31
  • 52
  • 1
    really big thank to you man i cant describe how much am i happy for your help ^_^ you already given me a new lesson , you are so kind :) and i appreciate it :) – Hamdi782 Oct 03 '14 at 21:44