Thread: Order of Operations Question

  1. #16
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Question system( );

    I would just like to see an example of a "literal string" where using 'system ( )' is acceptable to the general programming community.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #17
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Quote Originally Posted by The Brain
    I would just like to see an example of a "literal string" where using 'system ( )' is acceptable to the general programming community.
    Well, because a string literal is converted to a pointer when passed to a function, your request presumably allows pointers, so the only 100% portable usage of system is:
    Code:
    int has_command_processor = std::system(0);
    Everything else is implementation-defined.

  3. #18
    chix + guns > * chix/w/guns's Avatar
    Join Date
    Jul 2004
    Posts
    15
    I had another problem involving some sentences I wanted to concatenate into a paragraph using arrays. Like so:

    1. Ask for input.
    2. Check if the line is blank, if so, the user is done entering text and the program should terminate. If not, then follow with the code to append the sentence to the array.
    3. Take the previously saved sentences and put them in a temporary storage, then recreate an array that is 1 dimension longer to hold the new sentence. Then put the previously stored sentences in the temporary array in the new longer array. Then put the sentence input this run of the cin.getline in the end of the new and longer array.
    4. Go to step 1.

    Sorry if that is confusing, but the problem I run into is that with arrays of characters as in strings you have only one dimension (length). If you try and input that into a multidimensional array how do you refference how to put it in. Can't arrays only transfer to arrays with identical dimensions? Like when you try the following:

    Code:
    int main()
    {
         char temporary[80];
         char fullparagraph[80][5];
         cin.getline(temporary, 80, '\n');
         fullparagraph[80][0] = temporary[80];
    }
    From the code above you see what I am trying to do, but logically that wouldn't work... so how would I do it!?
    Quote Originally Posted by The Brain
    your code looks good

  4. #19
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    You were trying for something like this?
    Code:
    #include <cstdlib>
    #include <cstring>  // For strcpy
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char line[80];
      char para[5][80];
      int  it;
    
      // Get the lines of a paragraph
      // Terminate at N lines or end-of-file
      for (it = 0; it < 5; it++) {
        if (!cin.getline(line, sizeof line)) {
          break;
        }
        strcpy(para[it], line);
      }
      // Check for fatal errors
      if (it < 5 && !cin.eof()) {
        cerr<<"Something really bad happened!"<<endl;
        return EXIT_FAILURE;
      }
      // Display the final paragraph
      cout<<"The paragraph is: "<<endl;
      for (int j = 0; j < it; j++) {
        cout<< para[j] <<endl;
      }
    
      return EXIT_SUCCESS;
    }
    Note how strcpy is used to transfer the contents of the arrays. This is because (as you probably know) arrays can't be assigned with the assignment operator. para is declared to be an array of 5 arrays of 80 characters. The first dimension designates lines and the second dimension represents characters in a line. Also notice that the loop condition checks for array overflow while a conditional inside the loop breaks if getline fails. After the loop the code checks to see if getline failed due to end-of-file (as we want). If something else made it fail then we print an error because there's really not much else to do in such a simple program. Error checking is important, especially when using input status codes that could be informative or erroneous. The last loop uses i as the upper limit because the array might not have been filled up.

    I couldn't help but notice that you're using old-style C++. Life would be much easier for you if you migrated to a modern compiler that would properly support standard niceties. Then you could do this and not worry about lengths:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
      typedef vector<string> paragraph;
      string    line;
      paragraph para;
    
      // Get the lines of a paragraph
      // Terminate at end-of-file
      while (getline(cin, line)) {
        para.push_back(line);
      }
      // Check for fatal errors
      if (!cin.eof()) {
        cerr<<"Something really bad happened!"<<endl;
        return EXIT_FAILURE;
      }
      // Display the final paragraph
      cout<<"The paragraph is: "<<endl;
      paragraph::const_iterator it = para.begin();
      while (it != para.end()) {
        cout<< *it <<endl;
        ++it;
      }
    
      return EXIT_SUCCESS;
    }
    This is much safer. It lets you create paragraphs of arbitrary length with minimal effort (less than the constrained version above!). The functionality is the same: Lines are read until end-of-file, no arbitrary line limit or limit on the number of lines you'll see, the status of cin is checked as before, and the each line is printed out. I used iterators instead of indices for the printing simply because it's more modern. In this case, the following loop would do the same thing:
    Code:
    for (paragraph::size_type it = 0; it < para.size(); it++) {
      cout<< para[it] <<endl;
    }
    It's generally better to get into the habit of using iterators though. They're very flexible and you'll be happy you got used to them when you start powerusing the standard library.

  5. #20
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ...now to throw in the whole "the standards say you can leave return 0 out of main" argument...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #21
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Oh SALEM! Where are you, Salem?

  7. #22
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >...now to throw in the whole "the standards say you can leave return 0 out of main" argument...
    But they don't say you must leave it out.

    >Oh SALEM! Where are you, Salem?

  8. #23
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Major_small is referring to a common argument that gets sparked on this board about int main() vs void main(). A brief look at Salem's avatar will show you what I'm talking about.

  9. #24
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Quote Originally Posted by sean_mackrory
    Major_small is referring to a common argument that gets sparked on this board about int main() vs void main(). A brief look at Salem's avatar will show you what I'm talking about.
    I'm familiar with Salem's avatar, and the "void main gestapo" , but the reference is somewhat confusing as the discussion hasn't quite yet gone off on that tangent (though it wouldn't surprise me if it did).

  10. #25
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    It just did

  11. #26
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    void main OWNS!!!!!! If it wasn't for void main i don't think i could go on for another day
    Woop?

  12. #27
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    the only valid void in this thread is the one in your head...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #28
    Registered User
    Join Date
    Aug 2003
    Posts
    22
    Since when is using system(); a security issue? Yes, people could write their own 'pause' command and put a virus in it, but would they really want to considering its running on their own computer?

  14. #29
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    It makes it easier to attach spyware

  15. #30
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Spitball
    Since when is using system(); a security issue? Yes, people could write their own 'pause' command and put a virus in it, but would they really want to considering its running on their own computer?
    um, no... say I write program and name it 'Pause.exe'. then I put it in the win32 folder on YOUR computer while you're not watching... then you run you program and it calls 'pause'. your program can't tell the difference, so it runs the only pause.exe in the folder. your program runs mine which then connects to the internet and sends me every keystroke you type for the next month...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Numerical order with ifs question?
    By JDrake in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2006, 01:29 PM
  2. Order of operations question
    By nicomp in forum C++ Programming
    Replies: 9
    Last Post: 09-20-2006, 07:18 PM
  3. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  5. Question involving using math operations...
    By Screwz Luse in forum C Programming
    Replies: 6
    Last Post: 12-04-2001, 05:20 PM