#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.
Related
This is a program that counts how many letters and numbers a string has,but when I Press Enter to exit after entering,it has no response.
#include <iostream>
using namespace std;
int main()
{
char c;
int nums=0,chars=0;
while(cin>>c){
if(c>='0'&&c<='9'){
nums++;
}else if((c>='A'&&c<='Z')||(c>='a'&&c<='z')){
chars++;
}
}
printf("nums:%d\nchars:%d",nums,chars);
return 0;
}
Pressing enter does not end input from std::cin and std::cin stops when encountering a whitespace.
Better would be to use std::getline and std::isdigit as shown below:
int main()
{
int nums=0,chars=0;
std::string input;
//take input from user
std::getline(std::cin, input);
for(const char&c: input){
if(std::isdigit(static_cast<unsigned char>(c))){
nums++;
}
else if(std::isalpha(static_cast<unsigned char>(c))){
chars++;
}
}
std::cout<<"nums: "<<nums<<" chars: "<<chars;
return 0;
}
Demo
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;
}
The following code is for printing the elements of a matrix in spiral order. The program works fine. The problem, however, is that the test compiler against which I'm checking the program, doesn't accept trailing white spaces at the end of the output. Could anyone give me some ideas as to how I can get around the last white space being added at the output?
#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string input;
int value;
int matrixA[10][10],matrixB[10][10],result[10][10];
int k=0,l=0,m=0,n=0;
int i=0,j=0;
cout<<"Enter first matrix:\n";
while(true)
{
//Read string from keyboard
getline(std::cin,input);
if(input.empty())
{
break;
}
//Parse string using sring stream
stringstream ss(input);
j=0;
while (ss >> value)
{
matrixA[i][j]=value;
j++;
if (ss.peek()==' ')
ss.ignore();
}
i++;
}
//Assign row and column length
k=i;
l=j;
cout<<"Enter second matrix:\n";
i=0;
while(true)
{
//Read string from keyboard
getline(std::cin,input);
if(input.empty())
{
break;
}
//Parse string using sring stream
stringstream ss(input);
j=0;
while (ss >> value)
{
matrixB[i][j]=value;
j++;
if (ss.peek()==' ')
ss.ignore();
}
i++;
}
//Assign row and column length
m=i;
n=j;
bool first=true;
if(l==m)
{
//Multiplication logic
for(i=0;i<k;i++)
{
for(j=0;j<n;j++)
{
result[i][j]=0;
for(int d=0;d<m;d++)
{
result[i][j]=result[i][j]+matrixA[i][d]*matrixB[d][j];
}
}
}
cout<<"The product is:\n";
//Display matrix result
for(i=0;i<k;i++)
{
for(j=0;j<n;j++)
{
cout<<result[i][0];
}
for(j=1;j<n;j++)
{
cout<<' '<<result[i][j];
}
cout << "\n";
}
}
else
{
cout<<"The two matrices have incompatible dimensions.\n";
}
return 0;
}
Sorry for the bad typing!
#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;
}
I have problem with this question I don't know what is wrong with my code that I get Presentation Error every time I don't know what is the format of output can you help me to solve this question I am sorry that my code is a little confusing
here is the link of question http://sharecode.ir/section/problemset/problem/1208
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
int main()
{
string temp=" ";
bool cheak3=false,cheak4=false;
int n,num;
cin>>n;
while(n != 0)
{
if(cheak4 == true)
cout<<endl;
cheak4=true;
cin>>num;
cheak3=false;
string cheak1,cheak;
while(1)
{
if(num ==-1)
break;
getline(cin,temp);
for(int i=0 ; i<temp.size() ; i++)
{
if(temp[i] != ' ')
cheak.push_back(temp[i]);
else
{
reverse(cheak.begin(),cheak.end());
cheak1+=cheak;
cheak.clear();
if(cheak3 == true)
cheak1.push_back(' ');
}
}
reverse(cheak.begin(),cheak.end());
cheak1+=cheak;
cheak.clear();
num--;
if(cheak3 == true)
{
cheak1.push_back(' ');
cout<<cheak1<<endl;
cheak1.clear();
}
cheak3=true;
}
n--;
}
}
I believe the tricky part is you should print a blank line between the output blocks.
Your code has too complicated logic! Here is my solution to this problem:
#include <iostream>
#include <string>
using namespace std;
int main() {
int N, lines;
string word;
cin>>N;
while (N--) {
cin>>lines;
while (lines--) {
char end;
do {
cin >> word;
end = cin.get();
for (int i=word.length()-1;i>=0;i--) cout<<word[i];
cout << end;
} while (end != '\n');
}
if (N) cout << endl;
}
return 0;
}
The line if (N) cout << endl; makes sure you print a newline character for every output block except the last one (when N equals to 0).
After reading each word in a line, you can use cin.get(); in order to determine the next character. If it is a space, then print it and read the next word. Else if it is a \n print it and go to next line! :)