I would just like to see an example of a "literal string" where using 'system ( )' is acceptable to the general programming community.
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
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:Originally Posted by The Brain
Everything else is implementation-defined.Code:int has_command_processor = std::system(0);
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:
From the code above you see what I am trying to do, but logically that wouldn't work... so how would I do it!?Code:int main() { char temporary[80]; char fullparagraph[80][5]; cin.getline(temporary, 80, '\n'); fullparagraph[80][0] = temporary[80]; }
Originally Posted by The Brain
You were trying for something like this?
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.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; }
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:
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:#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; }
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.Code:for (paragraph::size_type it = 0; it < para.size(); it++) { cout<< para[it] <<endl; }
...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
Oh SALEM! Where are you, Salem?
>...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?
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).Originally Posted by sean_mackrory
It just did
void main OWNS!!!!!! If it wasn't for void main i don't think i could go on for another day
Woop?
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
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?
It makes it easier to attach spyware
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...Originally Posted by Spitball
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