Reading in spaces and editing chars in getline?
Mar 30, 2016 at 7:55pm UTC
I can't read in spaces to the array. But when I switch to getline I don't know how to edit out the vowels. Please help!
Trying to figure out the problem in the compress_file 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 128 129 130 131 132 133 134 135 136 137 138
#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 = " " ;
string lines;
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++;
}
}
Mar 31, 2016 at 3:27am UTC
cin does not collect spaces. In fact, it reads whatever you entered up until the first thing, then potentially throws everything else away. Use either an array of characters (c-string) or a string object.
Topic archived. No new replies allowed.