Basically, I would like to know two things, or how to go about doing the following, some tips, clues, or sample code to work from :)
1). How to allow a user to actually type in a file location, therefore allowing the program to ask cout << "please enter file location" << endl;... and then enter that into a variable, and use that variable as the file for the program to open and edit.
2). Like I have added 1 to each word, then 12, then 123. What if I wanted to change the first letter of each word to a capital letter?
#include <string>
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
char buffer[256]; //This will fail in cases where there are more than 256 bytes per line
cout << "Please enter path to the file: " << endl;
string path;
cin >> path;
fstream myfile(path.c_str());
while(!myfile.eof() )
{
myfile.getline(buffer, 100); //the way you have it now cout << buffer << "1" will add 1 to //each line, not to each word,
cout << buffer << "1" << endl;
cout << buffer << "12" << endl; //this will repeat the same line, only adding a 12 instead of
//a 1
}
}
what you probably want instead is:
1 2 3 4 5 6 7 8 9
while(!myfile.eof())
{
myfile >> buffer; //not sure this works with char arrays, you may want to change char buffer //to string buffer
buffer[0] = toupper(buffer[0]); //I forget which library offhand you have to include to use this //function
cout << buffer << "1" << endl;
myfile >> buffer; //you may want to clear the buffer first
buffer[0] = toupper(buffer[0]);
cout << buffer << "12" << endl;
}
myfile >> buffer; is what you want. It reads from the file until the first space is encountered. So the size of buffer will be however big each word is. (Assuming you declare buffer as string buffer).
Then you can modify each character in buffer with the index operator []. You can get the size of buffer with buffer.size() etc, and use a for loop to modify each character.
#include <fstream>
#include <iostream>
usingnamespace std;
int UserChoice(int x);
int main()
{
int a;
UserChoice(a);
string buffer;
cout << "Please enter path to the file: " << endl;
string fDirectory;
cin >> fDirectory;
fstream myfile (fDirectory.c_str());
while (!myfile.eof())
{
myfile >> buffer;
buffer[0] = toupper(buffer[0]);
if(a=1) {
cout << buffer << "1" << endl;
}
elseif(a=2){
cout << buffer << "12" << endl;
}
elseif(a=3){
cout << buffer << "123" << endl;
}
}
cout << "Press ENTER to continue..." << endl;
cin.get();cin.ignore();
return 0;
}
int UserChoice(int x) {
cout << "Welcome to the Word List Generator Verson 1.0" << endl;
cout << "Please choose from the following options: " << endl;
cout << "1. Append 1 to the end of each and every word and Capitalize first char" << endl;
cout << "2. Append 12 to the end of each and every word and Capitalize first char" << endl;
cout << "3. Append 123 to the end of each and every word and Capitalize first char." << endl;
cin >> x;
cout << "You chose option: " << x << endl;
return x;
}
Hey again, created a function which allows the user to choose an option (from 3), then returns it, I call the function in main, and insert an IF into my while loop, but the outcome is the same no matter what number I enter, whether it be 1, 2 or 3, it always, only appends 1 onto the end of each word. Any ideas what is wrong with my code?
1 other thing, change if (a=1), if (a=2), if (a=3) to if (a==1), if (a ==2), if (a==3) respectively. what you are doing now is assigned a to 1, a to 2, and a to 3.
/*
* File: main.cpp
* Author: Damien
*
* Created on 05 September 2011, 23:46
*/
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
usingnamespace std;
void centertext(char* s);
int UserChoice();
int main()
{
int a;
a = UserChoice();
if(a==4){
centertext("Thankyou for using Wordlist Generator 1.1!");
exit (1);
}
string buffer;
cout << "Please enter path to the file: " << endl;
string fDirectory;
cin >> fDirectory;
fstream myfile (fDirectory.c_str());
while (!myfile.eof())
{
myfile >> buffer;
buffer[0] = toupper(buffer[0]);
if(a==1) {
cout << buffer << "1" << endl;
}
elseif(a==2){
cout << buffer << "12" << endl;
}
elseif(a==3){
cout << buffer << "123" << endl;
}
}
return 0;
}
int UserChoice() {
int x;
cout << "Welcome to the Word List Generator Version 1.0" << endl;
cout << "Please choose from the following options: " << endl;
cout << "1. Append 1 to the end of each and every word and Capitalize first char" << endl;
cout << "2. Append 12 to the end of each and every word and Capitalize first char" << endl;
cout << "3. Append 123 to the end of each and every word and Capitalize first char." << endl;
cout << "4. Exit" << endl;
cin >> x;
cout << endl;
cout << "You chose option: " << x << endl;
cout << endl;
return x;
}
void centertext(char* s){
int l = strlen(s);
int pos = (int)((80-l)/2);
for(int i=0; i<pos; i++)
cout << " ";
cout << s << endl;
}
So I got the basics working, now I would like to move on, now I have modified the words, how can I write them to a new file or the existing file (or both), I assume using ofstream? and also allow the user to choose a name for the new .txt file being created.