#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
using namespace std;
int main()
{
string arr[3][2];
int i =0,j=0;
ofstream out ("test1.dat" , ios::app);
string name;
while(true)
{
cin>>name;
if(name=="end")
break;
out << name <<' ' ;
}
out.close();
ifstream in ("test1.dat", ios::in);
in >> name;
while(!in.eof())
{
arr[i][j]=name;
in>>name;
j++;
arr[i][j]=name;
i++;
j=0;
}
in.close();
for(i=0;i<3;i++){
cout<<endl;
for(j=0;j<2;j++){
cout<<arr[i][j]<<" ";}
}
return 0;
}
please help i have run-time error with this.what is my problem?i want to write some data in a file then read them and put them in an array and then print the array.i want to write and read the file by string not by character.sorry about my weak english.
thanks
Try adding a condition for i in your while loop.
This fixes your run-time error with the array bounds but you still have a logic error (unless you want duplicate occurrences of the same word in your array?)
while(!in.eof() && i < 3)
{
arr[i][j]=name;
in>>name;
j++;
arr[i][j]=name;
i++;
j=0;
}
Related
I have some question about file I/O in c++.
When I use while(fin>>x) for twice in my program and cout two times, only the first time will display on my screen.
And my test.txt is:
I like eat banana
I like eat apple
My code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
int main(){
ifstream fin;
fin.open("test.txt");
if(fin.fail()){
cout<<"Error!"<<endl;
exit(1);
}
else{
int i=0,j=0;
string x,y,a[20],b[20];
while(fin>>x){
a[i]=x;
i++;
}
fin.
while(fin>>y){
b[j]=y;
j++;
}
for(int q=0;q<20;q++){
cout<<a[q]<<" ";
}
for(int w=0;w<20;w++){
cout<<b[w]<<" ";
}
}
fin.close();
return 0;
}
The reason making fin >> x return false (and exiting the first loop) still exists when you write fin >> y right afterwards. Hence, once fin >> x-loop is left, fin >> y-loop will not be entered.
I'm trying to make a program that so far only needs to read a file and saves its content in an array. the cout was a test to see if the words would be saved to the array but it didn't work. When executed all it does is print to screen empty spaces and finally the name of the file.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <streambuf>
#include <ctime>
#include <time.h>
#define MAX 10000
void readFile(fstream& wordFile, string words[], int &wordarrayLength)
{
string word;
int i=0;
while(i < MAX)
{
getline(wordFile,word);
words[i] = word;
i++;
cout << words[i] << endl;
}
wordarrayLength = i;
wordFile.close();
}
int main()
{
string words[MAX];
int arraylength;
fstream file ("words.txt", ios::in);
readFile(file,words,arraylength);
}
I was missing the file the compiler was looking for. This code works fine.
Try something like this:
//since you are updating the array pass it in by reference as well.
void readFile(fstream& wordFile, string &words[], int &wordarrayLength)
{
int i=0;
while(i < MAX)
{
//each item in the array has to be it's own string
//the previous code simply reused the same string each time
string word;
getline(wordFile, word);
words[i] = word;
cout << words[i] << endl;
i++;
}
wordarrayLength = i;
wordFile.close();
}
Unfortunately I don't have your file or compiler so I can't debug it.
Yes, this is a question for a class, but I don't want my homework done for me. I just need to figure out where I'm going wrong. The question that I have to figure out states this:
Write a program which uses the file produced in Lab 1 as its input file. This program gets user input of a value from 1000 to 10000, and counts how many times the user's value appears in the input file. It reports to the user using screen output.
Here's the code that I have after several failed attempts:
#include <iostream>
#include <fstream>
using namespace std;
int count(int number, int input, int length)
{
int counter = 0;
for(int i = 0; i < length; i++)
if(input == number)
counter++;
return counter;
}
int main()
{
int num,input;
ifstream fin;
fin.open("ran_num.txt");
if(fin.fail())
{
cout<<"Input file opening failed."<<endl;
cin.get();
}
cout<<"Enter a number between 1000 to 10000:";
cin>>num;
fin>>input;
cout<<num<<"appears "<<count(num, input, 3000)<<" times in the file."<<endl;
return 0;
}
I'm completely lost and just need to figure out what I use to count a user inputer value. Any help is appreciated!
Edit
This is what I have now. My program can now read the whole file, but I still do not know how to count a user-inputted number.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream fin("ran_num.txt");
int num, user_input;
cout<<"Enter a number between 1000 to 10000:";
cin>>user_input;
while(fin>>num)
{
if(num == user_input)
{
cout<<count++;
}
}
return 0;
}
Are you sure your program is opening the file?
You should check if the file is opened correctly, you also should close the file after use.
Also you're outputting count before incrementing it.
I suggest changing your while loop this way:
if (fin.is_open())
{
while (fin >> num)
{
if (num == user_input)
{
++count;
}
}
fin.close();
cout << count;
}
else
{
cout << "Unable to open file";
}
This is the correct code that I have. It works perfectly now thank you!
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream fin("ran_num.txt");
int num, user_input, count;
cout<<"Please enter a number from 1000 to 10000:";
cin>>user_input;
if (fin.is_open())
{
while (fin >> num)
{
if (num == user_input)
{
++count;
}
}
fin.close();
cout<<user_input<<" occurs "<<count<<" times.";
}
else
{
cout<<"Unable to open file.";
}
fin.close();
return 0;
}
Hey guys I stuck working on an assignment in which I asked to write a program that lists the contents of a file.
#include<iostream>
#include<fstream>
using namespace std;
int main() {
string array[5];
ifstream infile("file_names.txt");
int x=0;
while(infile>>array[x++]){
for(int i=0;i<=x;i++){
infile >> array[i];
cout << array[i] << endl;}}
}
basically I have a file named "file_names.txt" that contains three strings and I want my program to list them.
you don't need two loops.
int main() {
int array_size=5;
string array[array_size];
ifstream infile("file_names.txt");
int x=0;int i=0;
while(i<array_size && infile>>array[i]){ //order is important here
cout << array[i] << endl;
i++;
}
}
Your assignment was
an assignment in which I asked to write a program that lists the contents of a file.
One way of printing the contents of a file could be
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream fin("my_file.txt", ios::in); // open input stream
if(!fin){ // check state, that file could be successfully opened
printf("Error opening file.");
return 1;
}
while(fin.peek() != EOF){
cout << (char)fin.get();
}
fin.close(); // close input stream
return 0;
}
This code demonstrates some basic C++ functionality like
opening an input stream, checking the state of the input stream and reading the contents character by character. Try to understand each step.
I know I can get to the same result like this
string array[50];
ifstream infile("file_names.txt");
for(int i=0; **i<3**; i++){
infile >> array[i];
cout << array[i] <<endl;}
But the whole point is to use a while loop because there might be more or less than 3 items
#include <iostream>
#include <vector>
#include <cstring>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
vector<string> Letter;
float frequency1(string word)
{
float count=0.0;
for (int i=0;i<Letter.size();++i)
{
transform(word.begin(),word.end(),word.begin(),::tolower);
transform(Letter[i].begin(),Letter[i].end(),Letter[i].begin(),::tolower);
if (strcmp(word.c_str(),Letter[i].c_str())==0)
{
count+=1;
}
}
count=(count/Letter.size())*100;
if (count>=0.5)
{
return count;
}
else
{
return 0.0;
}
}
int main()
{
ifstream fin;
fin.open("frequent.txt");
if (fin.fail())
{
cout<<"Error opening file!"<<endl;
}
while(!fin.eof())
{
string buffer;
getline(fin,buffer,' ');
cout<<buffer<<endl;
Letter.push_back(buffer);
}
cout<<endl;
vector<string> frequent;
vector<float> frequency;
for (int i=0;i<Letter.size();++i)
{
string a=Letter[i];
int k=0;
for (int j=0;j<i;++j)
{
transform(a.begin(),a.end(),a.begin(),::tolower);
transform(Letter[j].begin(),Letter[j].end(),Letter[j].begin(),::tolower);
if (a==Letter[j])
{
break;
}
k++;
}
int size=Letter.size();
if (k!=size-1)
{
continue;
}
float counter=frequency1(a);
if(counter>0.0)
{
frequent.push_back(Letter[i]);
frequency.push_back(counter);
}
}
cout<<"Here are the repeated words"<<endl;
for (int i=0;i<frequency.size();++i)
{
cout<<" "<<frequent[i]<<", frequency: "<<frequency[i]<<endl;
}
system("PAUSE");
return 0;
}
I am writing a program which determines the frequency of the repeated words in a document(text). If frequency is greater or equal to 0.5, the word passes as a repeated words. but when I run it, it doesn't show me any repeated word although I even calculated manually and know that there are repeated words in the document. I can't figure out the problem.
First, you should exit or wrap the remainder of main into an else, when you cannot open your input file
if (fin.fail())
{
cout<<"Error opening file!"<<endl;
}
Otherwise, you'd go on and try to read from an invalid stream.
You read your words with std::getline and a blank ' ' as delimiter. This means, that you include newlines '\n', tabs '\t', etc. in your words, which might not be what you intended. A better, and safer, approach to read your words, would be
std::string word;
while (fin >> word) {
// process word
}
This skips all whitespace and detects EOF properly as an added benefit.
There might be further problems as well.