Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualDarklink_

Posted 01 March 2012 - 08:32 AM

Ok, so I tried almost everything mentioned although finally I didn't realize what's the use of templates as the input of the name must always be a string. Anyway, of course you can use templates if you want but I don't see the use for this particular program. It just gets more complicated for the programmer. Do we gain more speed as the template executes each time it's called? Also can you elaborate a little what OP is?

Anyway, what I finally came up with is the following code which as long as I tested it works for every case. That is if the user inputs a string with a space in between characters, if there are multiple arguments of the same type in a line and if there isn't any argument in a line.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <fstream>
#include <sstream>
#include <map>

using namespace std;


class ReadGameFile {

public:

string line;
fstream file;
int count;
string args[7];


void enterDetails (){

for (count = 0; count <= 6; count++){

if (count == 0){ cout << "Please enter name: "; getline(cin, args[count]); }

else if (count == 1){ cout << "Please enter age: "; getline(cin, args[count]); }

else if (count == 2){ cout << "Please enter weight: "; getline(cin, args[count]); }

else if (count == 3){ cout << "Please enter class: "; getline(cin, args[count]); }

else if (count == 4){ cout << "Please enter race: "; getline(cin, args[count]); }

else if (count == 5){ cout << "Please enter hair color: "; getline(cin, args[count]); }

else if (count == 6){ cout << "Please enter pant size: "; getline(cin, args[count]); }

	}
  
	readFile();
  
}
void readFile (){

file.open("Rebels_after_start.txt");

while(file.good()){

  getline(file,line);

  repTags(line, args);

  cout << line << '\n';
}

file.close();

}
void repTags(string &line, string args[]){

static map<int,string> tags;
size_t pos = 0;

tags[0] = "/NAME/";
tags[1] = "/AGE/";
tags[2] = "/WEIGHT/";
tags[3] = "/CLASS/";
tags[4] = "/RACE/";
tags[5] = "/HAIR-COLOR/";
tags[6] = "/PANT-SIZE/";
  
	for (count = 0; count <= 6; count++){
	while ( (pos = line.find(tags[count])) != string::npos){

  line.replace(pos, tags[count].size(), args[count], 0, args[count].size());
  pos += args[count].size() + 1;

}// while
}//for
  

} // mapTEXT FUNCTION
}; // ReadGameFile CLASS

int main (){

ReadGameFile game;

game.enterDetails();

return 0;

}


So the output I get is something like this:


Hello Commander Shepard, you're looking mighty spry for a infiltrator of 26 years.
I see you are still dying your hair (I don't have any hair... :/), which is mightily becoming of a human like yourself.
You're putting on a few pounds, however. You look like you weigh about 70 lbs, and your belly is bulging out quite alot. Is that a size 32 pantaloons you are wearing?

You need to get into shape before your journey, Commander Shepard, if you don't mind me pointing out the obvious.



Now, I want to ask is there any other way to optimize the code and make it faster/better/more readable/more efficient?

If, so can you propose some ideas?

Again thanks a lot.

#4Darklink_

Posted 01 March 2012 - 08:31 AM

Ok, so I tried almost everything mentioned although finally I didn't realize what's the use of templates as the input of the name must always be a string. Anyway, of course you can use templates if you want but I don't see the use for this particular program. It just gets more complicated for the programmer. Do we gain more speed as the template executes each time it's called? Also can you elaborate a little what OP is?

Anyway, what I finally came up with is the following code which as long as I tested it works for every case. That is if the user inputs a string with a space in between characters, if there are multiple arguments of the same type in a line and if there isn't any argument in a line.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <fstream>
#include <sstream>
#include <map>

using namespace std;


class ReadGameFile {

public:


string line;
fstream file;
int count;


string args[7];void enterDetails (){

for (count = 0; count <= 6; count++){

if (count == 0){ cout << "Please enter name: "; getline(cin, args[count]); }

else if (count == 1){ cout << "Please enter age: "; getline(cin, args[count]); }

else if (count == 2){ cout << "Please enter weight: "; getline(cin, args[count]); }

else if (count == 3){ cout << "Please enter class: "; getline(cin, args[count]); }

else if (count == 4){ cout << "Please enter race: "; getline(cin, args[count]); }

else if (count == 5){ cout << "Please enter hair color: "; getline(cin, args[count]); }

else if (count == 6){ cout << "Please enter pant size: "; getline(cin, args[count]); }

	}
  
	readFile();
  
}
void readFile (){

file.open("Rebels_after_start.txt");

while(file.good()){

  getline(file,line);

  repTags(line, args);

  cout << line << '\n';
}

file.close();

}
void repTags(string &line, string args[]){

static map<int,string> tags;
size_t pos = 0;

tags[0] = "/NAME/";
tags[1] = "/AGE/";
tags[2] = "/WEIGHT/";
tags[3] = "/CLASS/";
tags[4] = "/RACE/";
tags[5] = "/HAIR-COLOR/";
tags[6] = "/PANT-SIZE/";
  
	for (count = 0; count <= 6; count++){
	while ( (pos = line.find(tags[count])) != string::npos){

  line.replace(pos, tags[count].size(), args[count], 0, args[count].size());
  pos += args[count].size() + 1;

}// while
}//for
  

} // mapTEXT FUNCTION
}; // ReadGameFile CLASS

int main (){

ReadGameFile game;

game.enterDetails();

return 0;

}


So the output I get is something like this:


Hello Commander Shepard, you're looking mighty spry for a infiltrator of 26 years.
I see you are still dying your hair (I don't have any hair... :/), which is mightily becoming of a human like yourself.
You're putting on a few pounds, however. You look like you weigh about 70 lbs, and your belly is bulging out quite alot. Is that a size 32 pantaloons you are wearing?

You need to get into shape before your journey, Commander Shepard, if you don't mind me pointing out the obvious.



Now, I want to ask is there any other way to optimize the code and make it faster/better/more readable/more efficient?

If, so can you propose some ideas?

Again thanks a lot.

#3Darklink_

Posted 01 March 2012 - 08:30 AM

Ok, so I tried almost everything mentioned although finally I didn't realize what's the use of templates as the input of the name must always be a string. Anyway, of course you can use templates if you want but I don't see the use for this particular program. It just gets more complicated for the programmer. Do we gain more speed as the template executes each time it's called? Also can you elaborate a little what OP is?

Anyway, what I finally came up with is the following code which as long as I tested it works for every case. That is if the user inputs a string with a space in between characters, if there are multiple arguments of the same type in a line and if there isn't any argument in a line.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <fstream>
#include <sstream>
#include <map>

using namespace std;


class ReadGameFile {
public:
string line;
fstream file;
int count;
string args[7];void enterDetails (){

for (count = 0; count <= 6; count++){

if (count == 0){ cout << "Please enter name: "; getline(cin, args[count]); }

else if (count == 1){ cout << "Please enter age: "; getline(cin, args[count]); }

else if (count == 2){ cout << "Please enter weight: "; getline(cin, args[count]); }

else if (count == 3){ cout << "Please enter class: "; getline(cin, args[count]); }

else if (count == 4){ cout << "Please enter race: "; getline(cin, args[count]); }

else if (count == 5){ cout << "Please enter hair color: "; getline(cin, args[count]); }

else if (count == 6){ cout << "Please enter pant size: "; getline(cin, args[count]); }

	}
  
	readFile();
  
}
void readFile (){

file.open("Rebels_after_start.txt");

while(file.good()){

  getline(file,line);

  repTags(line, args);

  cout << line << '\n';
}

file.close();

}
void repTags(string &line, string args[]){

static map<int,string> tags;
size_t pos = 0;

tags[0] = "/NAME/";
tags[1] = "/AGE/";
tags[2] = "/WEIGHT/";
tags[3] = "/CLASS/";
tags[4] = "/RACE/";
tags[5] = "/HAIR-COLOR/";
tags[6] = "/PANT-SIZE/";
  
	for (count = 0; count <= 6; count++){
	while ( (pos = line.find(tags[count])) != string::npos){

  line.replace(pos, tags[count].size(), args[count], 0, args[count].size());
  pos += args[count].size() + 1;

}// while
}//for
  

} // mapTEXT FUNCTION
}; // ReadGameFile CLASS

int main (){

ReadGameFile game;

game.enterDetails();

return 0;

}


So the output I get is something like this:


Hello Commander Shepard, you're looking mighty spry for a infiltrator of 26 years.
I see you are still dying your hair (I don't have any hair... :/), which is mightily becoming of a human like yourself.
You're putting on a few pounds, however. You look like you weigh about 70 lbs, and your belly is bulging out quite alot. Is that a size 32 pantaloons you are wearing?

You need to get into shape before your journey, Commander Shepard, if you don't mind me pointing out the obvious.



Now, I want to ask is there any other way to optimize the code and make it faster/better/more readable/more efficient?

If, so can you propose some ideas?

Again thanks a lot.

#2Darklink_

Posted 01 March 2012 - 08:29 AM

Ok, so I tried almost everything mentioned although finally I didn't realize what's the use of templates as the input of the name must always be a string. Anyway, of course you can use templates if you want but I don't see the use for this particular program. It just gets more complicated for the programmer. Do we gain more speed as the template executes each time it's called? Also can you elaborate a little what OP is?

Anyway, what I finally came up with is the following code which as long as I tested it works for every case. That is if the user inputs a string with a space in between characters, if there are multiple arguments of the same type in a line and if there isn't any argument in a line.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <fstream>
#include <sstream>
#include <map>
using namespace std;
class ReadGameFile {
public:
string line;
fstream file;
int count;
string args[7];void enterDetails (){

for (count = 0; count <= 6; count++){

if (count == 0){ cout << "Please enter name: "; getline(cin, args[count]); }

else if (count == 1){ cout << "Please enter age: "; getline(cin, args[count]); }

else if (count == 2){ cout << "Please enter weight: "; getline(cin, args[count]); }

else if (count == 3){ cout << "Please enter class: "; getline(cin, args[count]); }

else if (count == 4){ cout << "Please enter race: "; getline(cin, args[count]); }

else if (count == 5){ cout << "Please enter hair color: "; getline(cin, args[count]); }

else if (count == 6){ cout << "Please enter pant size: "; getline(cin, args[count]); }

	}
  
	readFile();
  
}
void readFile (){

file.open("Rebels_after_start.txt");

while(file.good()){

  getline(file,line);

  repTags(line, args);

  cout << line << '\n';
}

file.close();

}
void repTags(string &line, string args[]){

static map<int,string> tags;
size_t pos = 0;

tags[0] = "/NAME/";
tags[1] = "/AGE/";
tags[2] = "/WEIGHT/";
tags[3] = "/CLASS/";
tags[4] = "/RACE/";
tags[5] = "/HAIR-COLOR/";
tags[6] = "/PANT-SIZE/";
  
	for (count = 0; count <= 6; count++){
	while ( (pos = line.find(tags[count])) != string::npos){

  line.replace(pos, tags[count].size(), args[count], 0, args[count].size());
  pos += args[count].size() + 1;

}// while
}//for
  

} // mapTEXT FUNCTION
}; // ReadGameFile CLASS

int main (){

ReadGameFile game;

game.enterDetails();

return 0;

}


So the output I get is something like this:


Hello Commander Shepard, you're looking mighty spry for a infiltrator of 26 years.
I see you are still dying your hair (I don't have any hair... :/), which is mightily becoming of a human like yourself.
You're putting on a few pounds, however. You look like you weigh about 70 lbs, and your belly is bulging out quite alot. Is that a size 32 pantaloons you are wearing?

You need to get into shape before your journey, Commander Shepard, if you don't mind me pointing out the obvious.



Now, I want to ask is there any other way to optimize the code and make it faster/better/more readable/more efficient?

If, so can you propose some ideas?

Again thanks a lot.

#1Darklink_

Posted 01 March 2012 - 08:28 AM

Ok, so I tried almost anything mentioned although finally I didn't realize what's the use of templates as the input of the name must always be a string. Anyway, of course you can use templates if you want but I don't see the use for this particular program. It just gets more complicated for the programmer. Do we gain more speed as the template executes each time it's called? Also can you elaborate a little what OP is?

Anyway, what I finally came up with is the following code which as long as I tested it works for every case. That is if the user inputs a string with a space in between characters, if there are multiple arguments of the same type in a line and if there isn't any argument in a line.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <fstream>
#include <sstream>
#include <map>
using namespace std;
class ReadGameFile {
public:
string line;
fstream file;
int count;
string args[7];void enterDetails (){

for (count = 0; count <= 6; count++){
 
if (count == 0){ cout << "Please enter name: "; getline(cin, args[count]); }

else if (count == 1){ cout << "Please enter age: "; getline(cin, args[count]); }

else if (count == 2){ cout << "Please enter weight: "; getline(cin, args[count]); }

else if (count == 3){ cout << "Please enter class: "; getline(cin, args[count]); }

else if (count == 4){ cout << "Please enter race: "; getline(cin, args[count]); }

else if (count == 5){ cout << "Please enter hair color: "; getline(cin, args[count]); }

else if (count == 6){ cout << "Please enter pant size: "; getline(cin, args[count]); }
 
    }
   
    readFile();
   
}
void readFile (){

file.open("Rebels_after_start.txt");

while(file.good()){
 
  getline(file,line);
 
  repTags(line, args);
 
  cout << line << '\n';
}

file.close();

}
void repTags(string &line, string args[]){
 
static map<int,string> tags;
size_t pos = 0;

tags[0] = "/NAME/";
tags[1] = "/AGE/";
tags[2] = "/WEIGHT/";
tags[3] = "/CLASS/";
tags[4] = "/RACE/";
tags[5] = "/HAIR-COLOR/";
tags[6] = "/PANT-SIZE/";
   
    for (count = 0; count <= 6; count++){
    while ( (pos = line.find(tags[count])) != string::npos){

  line.replace(pos, tags[count].size(), args[count], 0, args[count].size());
  pos += args[count].size() + 1;
 
}// while
}//for
   

} // mapTEXT FUNCTION
}; // ReadGameFile CLASS

int main (){

ReadGameFile game;

game.enterDetails();

return 0;
 
}


So the output I get is something like this:


Hello Commander Shepard, you're looking mighty spry for a infiltrator of 26 years.
I see you are still dying your hair (I don't have any hair... :/), which is mightily becoming of a human like yourself.
You're putting on a few pounds, however. You look like you weigh about 70 lbs, and your belly is bulging out quite alot. Is that a size 32 pantaloons you are wearing?

You need to get into shape before your journey, Commander Shepard, if you don't mind me pointing out the obvious.



Now, I want to ask is there any other way to optimize the code and make it faster/better/more readable/more efficient?

If, so can you propose some ideas?

Again thanks a lot.

PARTNERS