My code compiles but for some reason there is one error that comes up:
Error:Multiple markers at this line - Invalid arguments ' Candidates are: void open(const char *, enum std::_Ios_Openmode) ' - no
matching function for call to 'std::basic_ifstream::open(std::__cxx11::string&)'
I cannot seem to figure out why this error is thrown.
Here's my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
int array_for_numbs[10];
int numbers[9]={1,2,3,4,5,6,7,8,9};
int counttarr[9]={0};
ifstream fileinpt;
int num, dgt;
string txtfile;
cout<<"enter the test file:";
cin >> txtfile;
fileinpt.open(txtfile);// this line is where the error pops up :(
int i=0;
while (!fileinpt.eof())
{
fileinpt >> array_for_numbs[i];
i=i+1;
}
fileinpt.close();
for(int i = 0; i < 10; i++)
{
num=array_for_numbs[i];
do
{
dgt=num%10;
num=num/10;
}while(num>0);
for(int i = 0; i < 9; i++)
{
if(dgt==numbers[i])
{
counttarr[i]=counttarr[i]+1;
}
}
}
cout<<"Digit \t"<< "Count \t"<<"Frequency "<<endl;
for(int i = 0; i < 9; i++)
{
float frq=(float)counttarr[i]/(float)100;
cout<<(i+1)<<"\t"<< counttarr[i]<<"\t" <<frq<<endl;
}
system("pause");
return 0;
}
"open" function filename argument has char* datatype, however, you are trying to pass std::string type path. Open function is a C function which is not aware of std::string type.
You have to cast/convert std::string to char*
Use;
fileinpt.open(txtfile.c_str());
Related
Writing a program that reads a set of strings into an array, and takes all the characters until whitespace occurs. Also, the strings are being read from the last to the first one from the array. My output looks really strange when I compile the code and read the output text file. Is there an error within the code here?
#include <iostream>
#include <stream>
using namespace std;
string substring(string a, int b, int c) {
string result = "";
for(int i = b; i<b+c; i++) {
result+=a[i];
}
return result;
}
int main() {
ifstream in_stream("HW3Test.txt");
ofstream output_stream("HW3output.txt");
string result[100];
int i=0;
while(!in_stream.eof()) {
getline(in_stream, result[i]);
i++;
}
for(int j = i; j>=0; j--) {
int counter1 = 0;
while(result[j][counter1]!=' ') {
counter1++;
}
output_stream<<substring(result[j], 0, counter1);
}
output_stream.close();
in_stream.close();
return 0;
}
#include <iostream>
using namespace std;
struct stud
{
char name[10];
int id;
};
int input(stud a[], int size)
{
for(int i=1; i<=size; i++)
{
cout<<"name = ";
cin>>a[i].name;
cout<<"id = ";
cin>>a[i].id;
}
cout<<endl;
return 0;
}
int output(stud a[], int size)
{
for(int i=1; i<=size; i++)
{
cout<<"name = "<<a[i].name<<" ";
cout<<"id = "<<a[i].id<<" ";
}
cout<<endl;
return 0;
}
int copy(stud a[], stud x[], int size)
{
for(int i=1; i<=size; i++)
{
x[i].name=a[i].name;
x[i].id=a[i].id;
}
output(x,size);
cout<<endl;
return 0;
}
int main()
{
struct stud s[3], x[3];
input(s,3);
output(s,3);
copy(s,x,3);
return 0;
}
In this program the statement in function copy x[i].name =a[i].name; is not copying contents from 1 structure object to another. I have tried to put this statement in for loop for(int j=1;j<=10;j++) x[i].name[j] =a[i].name[j]; but still not working.
please suggest what should be changed or some alternatives for this.
i'll be very thankful to you for this.
regards,
umar
Either using a loop to copy each character in the name field or using thestrcpy function from <cstring> header works.
int copy(stud a[], stud x[], int size) {
for(int i = 1; i <= size; i++) {
// for(unsigned j = 0; j < 10; j++) {
// x[i].name[j] = a[i].name[j];
// }
strcpy(x[i].name, a[i].name);
x[i].id = a[i].id;
}
output(x, size);
cout << endl;
return 0;
}
But since you tagged this as c++, consider using std::string instead of a char array, unless you have a particular reason for using a char array. In that case x[i].name = a[i].name would have worked just fine and you could also use the standard algorithm library for copy. Also, using std::array instead of a raw C array for you "array of structures" might be a better option (does not degenerate into a pointer like a regular C array does).
Evrey single one of your loops is wrong, because in C++ arrays start at zero. So not
for(int i=1; i<=size; i++)
instead
for(int i=0; i<size; i++)
You cannot copy arrays by writing a = b;. Since your arrays are really strings there's a built in function strcpy to copy strings.
strcpy(x[i].name, a[i].name);
If you use = to copy struct, the char array inside that struct will be copied. You don't need to do anything more.
#include <iostream>
using namespace std;
typedef struct{
char name[10];
} type_t;
int main() {
type_t a = {"hihi"};
type_t b;
b = a;
a.name[0] = 'a';
cout<<a.name<<endl;
cout<<b.name<<endl;
return 0;
}
output:
aihi
hihi
ideone: https://ideone.com/Zk5YFd
I am trying to write a c++ program to read a txt file containing data (122X300 matrix - tab delimited matrix) into my code and get it to display. The following is the code I wrote after referring extensively to google and many similar questions on this site. On running the code, I do not get any errors, however it does give me huge list of numbers which I cant seem to make any sense of. The following is the code: Any help would be great. I do not know where I am going wrong. Thanks.
DID some changes after considering the comment below by #ZekeMarsh, the problem now is that my text data is like:
Data Matrix Snapshot
The output I am getting is this:
Output of Code
The row counter does not move over to the next row,instead continues in the same row after incrementation....No idea why. The code modified is as follows:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;
int main(){
int HEIGHT = 3;
int WIDTH = 2;
int array_req[HEIGHT][WIDTH];
string userinputprompt, filename;
userinputprompt = "Data Filename: ";
cout<<userinputprompt<<endl;
getline(cin,filename);
ifstream inputfile;
inputfile.open(filename.c_str());
for(int i=0; i<HEIGHT; i++)
{
for(int j=0; j<WIDTH; j++)
{
/*if(!(inputfile>>array_req[i][j]))
{
cerr<<"Error";
break;
}
else if(!inputfile) // its error.. , can use a cerr here...
{
cerr<<"Error";
break;
}
else*/
inputfile>>array_req[i][j];
cout<<i<<","<<j<<"-->"<<array_req[i][j]<<endl;
}
/* This is not needed, read above comment
else
{
inputfile >> array_req[i][j];
}*/
}
for(int p=0; p<HEIGHT; p++)
{
for(int q=0; q<WIDTH; q++)
{
cout<<array_req[p][q]<<" ";
}
cout<<"\n";
}
inputfile.close();
getchar();
return 0;
}
.
EDITED CODE - The output array is a null matrix. Please help. What is wrong in the code ..compiles correctly. Trying to read line by line using getline and stringstream based on a lot of examples I read here..still not working.
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
#include <sstream>
#include <stdlib.h>
const int HEIGHT = 3;
const int WIDTH = 4;
const int BUFFSIZE = 10000;
using namespace std;
int main(){
int array_req [HEIGHT][WIDTH];
char buff[BUFFSIZE];
string userinputprompt, filename;
userinputprompt = "COLORDATA FILENAME: ";
cout<<userinputprompt<<endl;
getline(cin,filename);
ifstream inputfile;
stringstream ss;
inputfile.open(filename.c_str());
for (int i=0; i<HEIGHT; i++)
{
inputfile.getline(buff,BUFFSIZE,'\n');
ss<<buff;
for(int j=0;j<WIDTH; j++)
{
ss.getline(buff,1000,'\n');
array_req[i][j]=atoi(buff);
}
ss<<"";
ss.clear();
}
for(int p=0; p<HEIGHT; p++)
{
for(int q=0; q<WIDTH; q++)
{
cout<<array_req[p][q]<<" ";
}
cout<<"\n";
}
inputfile.close();
getchar();
return 0;
}
First of all during the printing of your array, you are not delimiting your data with anything which will result in a line of numbers. You should add delimiter and line breaks.
Second and most importantly: you try printing the full value of the array while it has not been filled up yet. I believe you meant to put the printing outside of the loop that works with your variable i . Now you are printing garbage in the places where the array was not filled yet.
Edit: Here is only the reading part as I believe that is only what you are looking for:
for (int i = 0; i < HEIGHT; ++i)
{
std::string tmpString;
std::getline(inputfile, tmpString);
std::stringstream ss(tmpString);
for(int j=0;j < WIDTH; ++j)
{
ss >> array_req[i][j];
}
}
This is the function that read the file
void read_function(istream& filename, vector< vector<double> >& v)
{
//vector<vector<double> > v;
if (filename)
{
string linea;
int i = 0;
while (getline(filename, linea))
{
v.push_back(vector<double>());
stringstream split(linea);
double value;
while (split >> value)
{
v.back().push_back(value);
}
}
}
};
The main function
int main(int argc, char const *argv[]){
if (argc < 2)
{
cerr << "input file's name\n"<< endl;
}
string program_name = argv[0];
ifstream input;
input.open(argv[1]);
vector< vector<double> > array;
for (int i = 0; i < array.size(); i++)
{
for (int j = 0; j < array[i].size(); j++)
cout << read_function(argv[1], array) << '\t';
cout << endl;
}
return 0;
}
When I compile the code I get the following errors messages
error: invalid initialization of reference of type ‘std::istream& {aka std::basic_istream&}’ from expression of type ‘const char*’
cout << read_function(argv[1], array) << '\t';
error: in passing argument 1 of ‘void read_function(std::istream&, std::vector >&)’
void read_function(istream& filename, vector< vector >& v)
Please dont take this the wrong way, but the errors you made in your code look like this might be a too difficult task for your level of programming skills.
You make a couple of mistakes:
You (kind of) declare input in main but try to use it in read_function
the parameters you use in getline are incorrect
the first parameter of read_function is of type ifstream not char*
getline is a member function of ifstream
read_function returns nothing, so cout<<read_function(...) is incorrect
I would suggest before trying to use more complicated things like sstream, fstream or vector you first try to understand how to call a function, what the parameters and their types are, what an object is and how to access its members.
I corrected your mistakes, the code below only reads in numbers from your input file, no characters. I assumed that was your goal, since you used a double vector. Also there are more elegant ways to do this, but I tried to stay as close to your code as possible.
code:
#include<fstream>
#include<iostream>
#include<vector>
#include<sstream>
using namespace std;
void read_function(char* filename, vector< vector<double> >& v)
{
int maxNumberOfCharsPerLine=1000;
ifstream input;
input.open(filename);
if(input.is_open())
{
char* inputChar;
string linea;
while (input.getline(inputChar, maxNumberOfCharsPerLine))
{
linea=inputChar;
v.push_back(vector<double>());
stringstream split(linea);
double value;
while (split >> value)
{
v.back().push_back(value);
}
}
}
input.close();
}
int main(int argc, char const *argv[]){
if (argc < 2)
{
cerr << "no input file's name\n"<< endl;
}
vector< vector<double> > array;
read_function((char*)argv[1], array);
for (int i = 0; i < array.size(); i++)
{
for (int j = 0; j < array[i].size(); j++)
{
cout << array[i][j] <<" ";
}
cout << endl;
}
return 0;
}
It's be awhile since I touched C++, but I'm writing inside my main I have a function called "solution" and right now that line is giving me the error: "a function definition is not allowed here before '{'"
Afterwards I thought I was supposed to write my funciton definitions after my main(), but that led to another slue of errors.
Also, as my code stands, I get the error of "invalid arguments" when I call my function solution and pass it to my outfile.
I also get the error on the final '{' of "expected '{' at end of input."
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
int main(int argc, char** argv) {
ifstream infile("TEST.txt", std::ifstream::in);
string line;
vector<string> inputLines;
if(infile.is_open()){
while(getline(infile, line)){
cout << line << '\n';
inputLines.push_back(line);
}
}
infile.close();
ofstream outfile("output.txt", std::ofstream::out);
for(unsigned int i = 1; i < inputLines.size(); i+= 3){
int credit = inputLines[i];
int numofItems = inputLines[i+1];
int numofItemscopy = inputLines[i+1];
vector<int> items;
stringstream ssin(inputLines[i+2]);
int x = 0;
while(ssin.good() && x < numofItems){
ssin >> items[x];
++x;
}
outfile << solution(credit,
numofItems,
numofItemscopy,
items.size());
outfile << inputLines[i] << '\n';
}
outfile.close();
return 0;
}
string solution(int cred, vector<int> original, vector<int> copy, int size){
for(int i = 0; i < size; i++ ){
for (int ii = 0; ii < size; ii++){
if(original[i] + copy[ii] == cred){
return std::string(i) + std::string(ii);
}
}
}
return "";
}
EDIT:
I put my solution function after my main, now I am getting the following errors:
On all three lines of :
int credit = inputLines[i];
int numofItems = inputLines[i+1];
int numofItemscopy = inputLines[i+1];
I get the error: "cannot convert ‘std::basic_string’ to ‘int’ initialization"
Also when I call my "solution" function:
outfile << solution(credit,
numofItems,
numofItemscopy,
items.size());
I get the error that "Solution was not declared in this scope."
First of all, you need to declare your function before you use it in your main function. You can then define it after your main function if you so desire. The compiler goes from top to bottom, and it only knows about what it has seen up to that point. The compiler has not yet seen the solution function when you call it in main, so it does not know what to do.
string solution(int cred, vector<int> original, vector<int> copy, int size);
Your declaration should look like this. To declare a function you just take its header and instead of giving it a body with {}, you just end the line with a ; like follows.
int num = stoi("32");
As for parsing strings to integers, in C++11 you can do that simply as seen above. See more info on that here: https://stackoverflow.com/a/11354496/2749485
See below for how your code should now look:
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
string solution(int cred, vector<int> original, vector<int> copy, int size);
int main(int argc, char** argv) {
ifstream infile("TEST.txt", std::ifstream::in);
string line;
vector<string> inputLines;
if(infile.is_open()){
while(getline(infile, line)){
cout << line << '\n';
inputLines.push_back(line);
}
}
infile.close();
ofstream outfile("output.txt", std::ofstream::out);
for(unsigned int i = 1; i < inputLines.size(); i+= 3){
int credit = stoi(inputLines[i]);
int numofItems = stoi(inputLines[i+1]);
int numofItemscopy = stoi(inputLines[i+1]);
vector<int> items;
stringstream ssin(inputLines[i+2]);
int x = 0;
while(ssin.good() && x < numofItems){
ssin >> items[x];
++x;
}
outfile << solution(credit,
numofItems,
numofItemscopy,
items.size());
outfile << inputLines[i] << '\n';
}
outfile.close();
return 0;
}
string solution(int cred, vector<int> original, vector<int> copy, int size){
for(int i = 0; i < size; i++ ){
for (int ii = 0; ii < size; ii++){
if(original[i] + copy[ii] == cred){
return std::string(i) + std::string(ii);
}
}
}
return "";
}
Function definitions should go before your main() if you want to have them in the same source file