Reading in spaces.
Mar 30, 2016 at 5:10am UTC
Below isn't the full code but it is where my problem is. I am trying to read in an input file, remove the vowels, and then output it. I am running into the problem where the spaced aren't being read into the array and I don't know how to get around it. I know I can use getline(), but I don't know how to use it where I can edit each character like I want to. My output file is just a bunch of weird characters. Help would be very much appreciated!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
int i = 0;
int x = 0;
string space = ' ' ;
while (!input.eof()){
input >> holding[i];
i++;
if (holding[x] == 'A' || holding[x] == 'a' ||holding[x] == 'E' ||
holding[x] == 'e' || holding[x] == 'I' || holding[x] == 'i' ||
holding[x] == 'O' || holding[x] == 'o' || holding[x] == 'U' ||
holding[x] == 'u' || holding[x] == 'Y' || holding[x] == 'y' ){
x++;
}
if (holding[x] == ' ' ){
output << space;
x++;
}
else {
output << holding[x];
x++;
}
}
}
Mar 30, 2016 at 7:27am UTC
I did not look at your problem to close.
could you provide a complete example that demonstrates your problem in program that compiles?
Mar 30, 2016 at 8:23am UTC
Ignore the last function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
void get_input(string &input_filename);
void menu(int &choice);
void copy_file(string &input_filename, string &output_filename);
void compress_file(string &input_filename, string &output_filename);
void reverse_file(string &input_filename, string &output_filename);
int main(){
int choice;
string input_filename, output_filename;
get_input(input_filename);
menu(choice);
if (choice == 1){
copy_file(input_filename, output_filename);
}
if (choice == 2){
compress_file(input_filename, output_filename);
}
if (choice == 3){
reverse_file(input_filename, output_filename);
}
return 0;
}
void get_input(string &input_filename){
cout << "What is the name of the input file: " ;
cin >> input_filename;
}
void menu(int &choice){
cout << "Would you like to:" << endl;
cout << "1. Copy the input file." << endl;
cout << "2. Compress the file." << endl;
cout << "3. Reverse the file." << endl;
cout << "4. Merge two files." << endl;
cout << "5. Exit the program." << endl;
cout << "Choice (1-5): " ;
cin >> choice;
}
void copy_file(string &input_filename, string &output_filename){
cout << "Enter the name you'd like the copied file to have: " ;
cin >> output_filename;
ifstream input;
input.open(input_filename.c_str());
ofstream output;
output.open(output_filename.c_str());
string lines;
while (!input.eof()){
getline(input, lines);
if (lines != "" ){
output << lines << endl;
//if statement that ignores spaces
}
}
}
void compress_file(string &input_filename, string &output_filename){
cout << "Enter the name you'd like the compressed file to have: " ;
cin >> output_filename;
char holding[10000];
ifstream input;
input.open(input_filename.c_str());
ofstream output;
output.open(output_filename.c_str());
int i = 0;
int x = 0;
string space = " " ;
while (!input.eof()){
input >> holding[i];
i++;
if (holding[x] == 'A' || holding[x] == 'a' ||holding[x] == 'E' ||
holding[x] == 'e' || holding[x] == 'I' || holding[x] == 'i' ||
holding[x] == 'O' || holding[x] == 'o' || holding[x] == 'U' ||
holding[x] == 'u' || holding[x] == 'Y' || holding[x] == 'y' ){
x++;
}
if (holding[x] == ' ' ){
output << space;
x++;
}
else {
output << holding[x];
x++;
}
}
}
void reverse_file(string &input_filename, string &output_filename){
cout << "Enter the name you'd like the reversed file to have: " ;
cin >> output_filename;
char holding[10000];
ifstream input;
input.open(input_filename.c_str());
ofstream output;
output.open(output_filename.c_str());
int i = 0;
while (!input.eof()){
input >> holding[i];
i++;
}
}
Topic archived. No new replies allowed.