Hi, so I'm trying to open a file and read the sentences in it line by line and put them into string and then put that string into a function.
So far I have this:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
ifstream file ("engsent"); //file just has some sentences
if (!file) {
cout << "unable to open file";
return false;
}
string sent;
while (getline (file, sent)){
cout << sent << endl;
}
//function goes here...
i have a function but not sure how to connect it so that when the file is opened it takes the first line/sentence and puts it into the function and then takes the second line/sentence and puts it into the same function, etc. until all the sentences in the file has gone through the function.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
void processLine(const string& line)
{
// use line
}
int main(){
ifstream file ("engsent"); //file just has some sentences
if (!file) {
cout << "unable to open file";
returnfalse;
}
string sent;
while (getline (file, sent))
{
cout << sent << endl;
processLine(sent);
}
hey thanks for the help! I tried it and it opened the files but instead it just printed out each line for me, I'm trying to get each line so that all the characters will be lowercase. I'm not using any advanced method because I'm not there yet
#include<iostream>
#include<string>
#include<fstream>
usingnamespace std;
void processLine(const string & line) {
// use line
}
int main() {
string word1, word2, word3, word4;
ifstream file("engsent"); //file just has some sentences
if (!file) {
cout << "unable to open file";
returnfalse;
}
string sent;
while (getline(file, sent)) {
cout << sent << endl;
processLine(sent);
}
//takes each word from a string and stores them into separate strings
int pos = 0, pos2, pos3, pos4;
//WORD1
while (sent[pos] != ' ') { //pos is 0 and starts from the beginning of string
word1 += sent[pos]; //stores word1 until the space
pos++;
}
//WORD2
for (pos2 = pos; sent[pos2] == ' '; pos2++); //finds the next word after word1 (word2)
while (sent[pos2] != ' ') {
word2 += sent[pos2];
pos2++;
}
//WORD3
for (pos3 = pos2; sent[pos3] == ' '; pos3++); //finds the next word after word2 (word3)
while (sent[pos3] != ' ') {
word3 += sent[pos3];
pos3++;
}
//WORD4
for (pos4 = pos3; sent[pos4] == ' '; pos4++); //finds the next word after word3 (word4)
while (sent[pos4] != '.') {
word4 += sent[pos4];
pos4++;
}
//takes each word/character and converts them all to lowercase
//WORD1
for (int i = 0; i < word1.length(); i++) {
if (word1[i] >= 'A' && word1[i] <= 'Z') { //checks if each character is lowercase
word1[i] = char(((int) word1[i]) + 0x20); //changes uppercase to lowercase
}
}
//WORD2
for (int i = 0; i < word2.length(); i++) {
if (word2[i] >= 'A' && word2[i] <= 'Z') {
word2[i] = char(((int) word2[i]) + 0x20);
}
}
//WORD3
for (int i = 0; i < word3.length(); i++) {
if (word3[i] >= 'A' && word3[i] <= 'Z') {
word3[i] = char(((int) word3[i]) + 0x20);
}
}
//WORD4
for (int i = 0; i < word4.length(); i++) {
if (word4[i] >= 'A' && word4[i] <= 'Z') {
word4[i] = char(((int) word4[i]) + 0x20);
}
}
//print out the sentence in all lowercase
cout << word1 << " " << word2 << " " << word3 << " " << word4 << endl;
return 0;
}
p.s. i tried this with a user input and it works but not sure how to go about it for an input file
I tried it and it opened the files but instead it just printed out each line for me, I'm trying to get each line so that all the characters will be lowercase.
That's all your program does, since processLine does nothing.
Where the comments say // use line is where your code to deal with a line should be.
yes, thank you that cleared that part up for me. and yeah, i'm restricted to the basics so i can't use tolower in my program or other libraries and vectors (basically have to do it the long way since my class haven't gotten to that part yet). Thanks!