GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Oct-2009, 00:00
satansoldier satansoldier is offline
New Member
 
Join Date: Oct 2009
Location: Washington
Posts: 2
satansoldier is on a distinguished road

Strings in system()


How can I get a string to work in system()?
CPP / C++ / C Code:
// Allows user to specify number of times to run
// the windows disk defrag utility on C:
#include <string>
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
  int loop = 0;
  int alpha;   // alpha is how many times the user wants to run it
  string drive;
  cout<<"Please enter the drive letter you would like to defrag.\n(enter a single letter)\n";
  cin>>drive;
  string defrag  = "defrag " + drive + ": -f";
  string anylize = "defrag " + drive + ": -a -v";
  string numbers = "Please enter a number between 0 and 20\n";
  string plural = " "; // see below
  cout<<"Anylizing your "<<drive<<": drive, Please Wait.\n\n";
  system(anylize);   // sends command to system
  cout<<"\nHow many times do you want to defrag?\n";
top:
  cin>>alpha;
  if (alpha == 0)          // If the user inputs 0, program ends.
   {
    goto end;
   }
  else
  {
    if (alpha < 0){
      cout<<numbers;
      goto top;   }
    else                   // 0>alpha>20 informs user of cap and allows new input
    {
      if (alpha > 20) {
        cout<<numbers;
        goto top;     }
      else
      {
        system("cls");
        while (loop<alpha) // loops until it reaches the user's input
         {
          loop++;
          if (loop==1){    // if/else to use correct plurality of "pass"
            plural = " ";}
          else {
            plural ="es ";}
          system(defrag);
          system("cls");
          cout<<loop<<" Pass"<<plural<<"completed.\n\n";
         }
      }
    }
  }
end:
cout<<"\nAll done.\nPress enter to exit.\n";
cin.get();
}
I realize the nested if/else's are a little messy but I can fix that later.
currently I get the error: cannot convert `std::string' to `const char*' for argument `1' to `int system(const char*)'|
  #2  
Old 06-Oct-2009, 05:58
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 350
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: strings in system()


CPP / C++ / C Code:
const char* std::string::c_str();


MxB
  #3  
Old 06-Oct-2009, 08:30
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 388
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: strings in system()


An online reference such as www.cplusplus.com can come in handy at times. For example, at times like these. The first question you can ask is "is there a standard way to do what I want to do?" Then you can go to the reference and see if there is.

Other than that, you'd probably do well to adopt some "better" coding practises right from the start.

I personally find that goto/longjmp makes the code harder to follow. I'm not probably the only one, since goto is considered a Bad Thing in so many places. My recommendation would be to start without and when you know you need it, use it.

Descriptive names / commenting is very important. Consider your variable 'alpha' for example. Your comment says "alpha is how many times the user wants to run it." The variable name, alpha, has absolutely no connection to the usage of the variable. More descriptive names would be 'numberOfRuns', 'timesToLoop' or similar. The same goes for 'defrag', 'anylize' (you probably meant 'analyze'?), 'numbers', 'plural'... More descriptive names could be 'defragCommand', 'analyzeCommand' or perhaps 'defrag_defrag', defrag_analyze'. The 'defrag' in the beginning separated with an underscore tries to give a hint that the variable holds the defragment command of the defrag program.

Here's an idea of how I could write your program. (I wouldn't actually do it like that, I was just lazy and "fixed" a few things to look more like my ideals.)
CPP / C++ / C Code:
// Allows user to specify number of times to run
// the windows disk defrag utility on C:
#include <string>
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
  int loop = 0;
  int numberOfRuns;   // numberOfRuns is how many times the user wants to run it
  string drive;
  cout<<"Please enter the drive letter you would like to defrag.\n(enter a single letter)\n";
  cin>>drive;
  string defrag  = "defrag " + drive + ": -f";
  string anylize = "defrag " + drive + ": -a -v";
  string numbers = "Please enter a number between 0 and 20\n";
  string plural = " "; // see below
  bool done = false;
  cout<<"Anylizing your "<<drive<<": drive, Please Wait.\n\n";
  system(anylize.c_str());   // sends command to system
  cout<<"\nHow many times do you want to defrag?\n";
 while (!done)
{
  cin>>numberOfRuns;
  if (numberOfRuns == 0)          // If the user inputs 0, program ends.
   {
    done = true;
   }
  else
  {
    if (numberOfRuns < 0){
      cout<<numbers;
    }
    else                   // 0>numberOfRuns>20 informs user of cap and allows new input
    {
      if (numberOfRuns > 20) {
        cout<<numbers;
        }
      else
      {
        system("cls");
        while (loop<numberOfRuns) // loops until it reaches the user's input
         {
          loop++;
          if (loop==1){    // if/else to use correct plurality of "pass"
            plural = " ";}
          else {
            plural ="es ";}
          system(defrag.c_str());
          system("cls");
          cout<<loop<<" Pass"<<plural<<"completed.\n\n";
         }
         done = true;
      }
    }
  }
}
cout<<"\nAll done.\nPress enter to exit.\n";
cin.get();
}
  #4  
Old 06-Oct-2009, 08:39
satansoldier satansoldier is offline
New Member
 
Join Date: Oct 2009
Location: Washington
Posts: 2
satansoldier is on a distinguished road

Re: strings in system()


Thanks. I do know that my code is a mess and there are several better ways to do it, I'm new to c++ so i'm just using what I already know with a few things i dont.
  #5  
Old 07-Oct-2009, 08:59
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 388
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: strings in system()


Quote:
Originally Posted by satansoldier
there are several better ways to do it

There usually are, but isn't it still helpful when people try to point out the "bad" things and recommend the "good" things.
  #6  
Old 13-Oct-2009, 23:55
george135 george135 is offline
Account Disabled
 
Join Date: Jan 2008
Posts: 10
george135 has a little shameless behaviour in the past

Re: strings in system()


Quote:
Originally Posted by Kimmo
There usually are, but isn't it still helpful when people try to point out the "bad" things and recommend the "good" things.

Then you should recommend good things instead : system() is dirty and prohibited on Windows (MSDN)
You must use Win32 System apis to defrag a disk.
  #7  
Old 14-Oct-2009, 06:20
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 388
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: strings in system()


Quote:
Originally Posted by george135
Then you should recommend good things instead : system() is dirty and prohibited on Windows (MSDN)
You must use Win32 System apis to defrag a disk.
But that would be like using std::string to solve a problem with const char* to char* conversion. It just won't do.

Actually, I have no idea how you would do it using WIN32 API, so I cannot recommend such a way. But perhaps you can show how it's done?
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
General Purpose Computer System vs. Embedded System brookeville Computer Software Forum - Windows 5 09-Mar-2009 04:10
Attempting to restore system BIOS on Dell Optiplex. brandon1482 Computer Hardware Forum 3 27-Jun-2008 07:43
Java application for a vehicle parking system asdfg Java Forum 10 17-Jun-2007 11:58
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
need help with a console menu system BullBuchanan C++ Forum 6 20-Aug-2006 14:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:59.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.