Skip to main content
GameDev.net gamedev.net
🔒 Locked

code error

Started by mecrazy Apr 11, 2005 at 9:33 AM 6 replies 1.1k views
Original Post
mecrazy
mecrazy
Hey I havo another code error for you guys. The book im learning from is accelerated c++. In the first exercise it asks for me to write a prgram that outputs - #include int main() { std::cout<<"hello, world!"<<std::endl; system("pause"); return 0; } as text instead of a program. I cannot work out how to do it can you guys give me a hand.
Accelerated C++
Fruny
Fruny
If you've figured out how to print out "hello world", you can easily adapt your code to print out arbitrary text... hello world's source code included. [wink]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
garyfletcher
garyfletcher
Do you mean that you need to read in the source file of the hello world program and print each line?
Gary.Goodbye, and thanks for all the fish.
smart_idiot
smart_idiot
I read 'outputs -' and thought he wanted it to echo stdin.

Oh, and a hint:
char text[] = "a line\n"              "another line\n"              "etc. . .";
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Mawr
Mawr
He could mean inputing the source and ouputting that to STDOUT? But that doesn't seem to be a first exercise. But, for fun 'n stuff, here is is:

#include <cstdlib>#include <iostream>#include <fstream>#include <string>using namespace std;int main(int argc, char *argv[]){    //argc is the amount of arguments given to this program    //while argv is those arguments    if(argc < 2)               //not enough arguments, exit    {      cout << "Usage: this.exe input_file" << endl;      cout << "Args: " << argc << endl;      return 0;    }        ifstream in;               //declare a input stream    in.open(argv[1], ios::in); //use the second argument as the file to read    in.seekg(0, ios::end);     //seek to the end of the file    int size = in.tellg();     //get the amount of bytes seeked    in.seekg(0, ios::beg);     //return the beginning of the file        if(size <= 0)              //if the file is empty, exit     {                          //also, bad files give a negative value for size      cout << "Bad or empty file.. exiting" << endl;      return 0;    }        string line="";            //declare a string    while(!in.eof())           //while we haven't reached the end of file    {      getline(in, line);       //read in till we reach a '\n'      cout << line << endl;    //output this to the screen    }    in.close();                //close the input stream we opened    system("PAUSE");               return 0;}


Hope the comments help.
PiCkLeD
PiCkLeD
Quote: "In the first exercise it asks for me to write a prgram that outputs ..."

Remind me never to buy buy this book!

If the 'first exercise' of a c++ book that promotes using streams before discussing basics, then there is hope for me publishing sometime soon :P
smart_idiot
smart_idiot
Slightly shorter version of Mawr's example:

#include <iostream>#include <fstream>int main(int argc, char *argv[]) {  using namespace std;    if(argc != 2) // Wrong number of arguments, exit.   {    cout << "Usage: " << argv[0] << " [input_file]" << endl;    return -1;   }    ifstream in(argv[1]); // Open the file.    if(in) // If the file is good for I/O operations, we'll copy it's buffer to cout:   cout << argv[1] << " contains: " << endl << in.rdbuf() << endl;  else   cout << "Couldn't open " << argv[1] << "." << endl;    // No need to close the file, ifstream will do that in it's destructor.    return 0; }
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Zahlman
Zahlman
Quote:
Original post by PiCkLeD
Quote: "In the first exercise it asks for me to write a prgram that outputs ..."

Remind me never to buy buy this book!

If the 'first exercise' of a c++ book that promotes using streams before discussing basics, then there is hope for me publishing sometime soon :P


In C++, streams are "the basics" - at least as far as using them on a "cout <<" level. Stop thinking in C. [smile]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.