Here is the code
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
class sudoku {
public:
void read(ifstream ifs);
void write(ofstream ofs);
private:
int puzzle[9][9];
};
void sudoku::read(ifstream ifs){
int row;
int col;
int value;
int linenum = 1;
bool error = false;
while(ifs >> row){
ifs >> col;
ifs >> value;
if(row < 0 || row > 8){
cerr << "Incorrect Row Value: " << row << " Line Number: " << linenum;
error = true;
}
if(col < 0 || col > 8){
error = true;
cerr << "Incorrect Col Value: " << col << " Line Number: " << linenum;
}
if(value < 1 || value > 9){
error = true;
cerr << "Invalid Value: " << value << " Line Number: " << linenum;
}
if (! error)
puzzle[row][col] = value;
error = false;
}
}
void sudoku::write(ofstream ofs){
for (int i = 0; i < 9; i++){
for (int j = 0; j < 0; j++){
if(puzzle[i][j] != 0){
ofs << i << ' ' << j << ' ' << puzzle[i][j] << endl;
}
}
}
}
int main(int argc, char* argv[]){
sudoku sudopuzzle;
string filename = argv[1];
int found = filename.find(".txt");
if(found == filename.npos) {
cout << "No .txt extension" << endl;
return 0;
}
ifstream ifs;
ifs.open(filename.c_str());
sudopuzzle.read(ifs);
ifs.close();
filename.resize(filename.size()-4);
filename.append("_checked.txt");
ofstream ofs;
ofs.open(filename.c_str());
sudopuzzle.write(ofs);
ofs.close();
return 0;
}
This program is supposed to read a generated puzzle. make sure its valid and write it to another file. Normally I am good at figuring out errors but this one is a mess. It spits out stuff referring to the inclusion of iostream and cites files that I have nothing to do with. Im guessing its some error that comes with passing fstreams to funcitons or something. any clue?
You should be passing a reference to the stream objects:
class sudoku {
public:
void read(ifstream &ifs);
void write(ofstream &ofs);
private:
int puzzle[9][9];
};
void sudoku::read(ifstream &ifs){
// sudoku::read code here
}
void sudoku::write(ofstream &ofs){
// sudoku::write code here
}
This change is required because both ifstream and ofstream have a =delete copy constructor. (Hat tip: #awesomeyi)
Related
I have a project that requires me to output the data from 2 files. Put them into their own arrays. Calculate the data from 1 of the files into a function. Then output the functions into a whole new file. This is what I have so far and right now I'm trying to figure out how to output the function output into a new file. When I try now, I get outputs in the file like "NULL NULL". I also have to align the data in columns.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
char states();
char stData();
char states()
{
const int SIZE = 0;
ifstream myfile("csc114_states.txt");
string line, myArray[SIZE];
if (myfile.is_open())
{
for (int i = 1; i < SIZE; i++)
{
myfile >> myArray[i];
cout << myArray[i];
cout << endl;
}
myfile.close();
}
myfile.open("csc114_states.txt");
if (myfile.is_open())
{
string tp;
while (getline(myfile, tp))
{
cout << tp << "\n";
}
myfile.close();
}
else
{
cout << "Unable to open file." << endl;
return 0;
}
return 0;
}
char stDATA()
{
const int size1 = 0;
const int size2 = 0;
string myArray2[size1][size2], line;
ifstream myfile2("csc114_data.txt");
if (myfile2.is_open())
{
for (int i = 0; i < size1; i++)
{
for (int j = 0; j < size2; j++)
{
myfile2 >> myArray2[i][j];
cout << myArray2[i][j];
cout << endl;
}
}
myfile2.close();
}
myfile2.open("csc114_data.txt");
if (myfile2.is_open())
{
string tp2;
while (getline(myfile2, tp2))
{
cout << setw(0) << tp2 << "\n";
}
myfile2.close();
}
else
{
cout << "Unable to open file." << endl;
return 0;
}
return 0;
}
int main()
{
ofstream bigFile("csc114_output.txt");
if (bigFile.is_open())
{
bigFile << states() << stDATA();
bigFile.close();
}
else
{
cout << "Unable to open files.";
}
return 0;
}
I am trying to create a program to combine two matrices. The first is to add them, the second is to subtract them, but I am getting an 1120 error. I don't know if that's because my code is bad, or if there's something wrong with my compiler. Here's what I've tried.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream matrix_file("matricies.txt");
fstream result1_file("result1.txt");
fstream result2_file("result2.txt");
matrix_file.open("matricies.txt", ios::in);
if (!matrix_file) {
cout << "File was not opened!" << endl;
}
else {
cout << "File opened successfully!";
}
result1_file.open("result1.txt", ios::app);
if (!result_file) {
cout << "File was not created!" << endl;
}
else {
cout << "File created successfully!";
}
result2_file.open("result1.txt", ios::app);
if (!result_file) {
cout << "File was not created!" << endl;
}
else {
cout << "File created successfully!";
}
int matrix1[9];
int matrix2[9];
int result1Matrix[9];
int result2Matrix[9];
for (int i = 0; i < 9; ++i)
{
matrix_file >> matrix1[i];
}
for (int i = 0; i < 9; ++i) {
matrix_file >> matrix2[i];
}
for (int i = 0; i < 9; ++i) {
result1Matrix[i] = matrix1[i] + matrix2[i]
}
for (int i = 0; i < 9; ++i) {
result2Matrix[i] = matrix1[i] - matrix2[i]
}
for (int i = 0; i < 9; ++i) {
result1_file >> result1Matrix[i] ;
}
for (int i = 0; i < 9; ++i) {
result2_file >> result2Matrix[i];
}
matrix_file.close();
result1_file.close();
result2_file.close();
}
Line 35, 38: expected ';' in end line
Output file (result_file) must use operator '<<' instead of '>>'.
Consider this program:
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
fstream file;
int line_count = 0;
int months;
string name_file;
std::cout << "Please input the file name for your report file: ";
std::cin >> name_file;
if (name_file != "load_report.txt")
{
std::cout << "Invalid report file!\n";
exit(1);
}
std::cout << "Please provide the number of months to consolidate: ";
std::cin >> months;
if (months <= 0)
{
std::cout << "You need to have at least one month in order to"
<< " consolidate your sales.\n";
exit(1);
}
std::cout << "\n";
file.open(name_file, ios::in);
if (file.is_open())
{
string line;
while (getline(file, line))
{
line_count++;
}
}
else
{
std::cout << "Could not open file!\n";
exit(1);
}
file.close();
file.open(name_file, ios::in);
vector<float> a(line_count);
int k = 0;
if (file.is_open())
{
for (auto & val : a)
{
file >> val;
}
}
else
{
std::cout << "Could not open file!\n";
exit(1);
}
file.close();
int loops = line_count / months;
for (int i = 0; i < loops; ++i)
{
float sum = 0;
std::cout << "Month ";
for (int j = 0; j < months; ++j)
{
std::cout << i * months + j + 1 << " ";
sum += a[i * months + j];
}
std::cout << "sales: $" << sum << "\n";
}
return 0;
}
Running make stylecheck returns the following:
16369 warnings generated.
/home/bl71/bl71/prob-02/prob-02-main.cpp:64:17: warning: calling a function that uses a default
argument is disallowed [fuchsia-default-arguments-calls]
vector<float> a(line_count);
^
/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:507:29: note: default
parameter was declared here
vector(size_type __n, const allocator_type& __a = allocator_type())
^
Is there an argument that can be passed through to make this warning go away? This is a program I am trying to write that loads a monthly report from a report file and displays the consolidated sales for a given range of months. In my directory, a load_report.txt file was manually created and I use this program to read its contents of that file using fstream. make stylecheck checks for internal documentation guidelines.
Im working on this program to read in students first, last name, and 5 grades and put the students info into a struct. From there Im trying to use other functions to find the average grade, letter grade, max grade, and minimum grade. Im having issues on the proper way to read in and store students info and then call upon students info within other functions to calculate average, letter grade, etc... My 'displayAverages' function doesn't list any names and the grades are huge negative numbers. IF you can help me (after your headache from looking at my code goes away that is) Id appreciate it.
#include "pch.h"
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <sstream>
using namespace std;
// Global variables
const int MAX_STUDENTS = 22;
const int MAX_GRADES = 5;
const string FILENAME = "NamesGrades.txt";
struct Student{
string name;
double grades[MAX_GRADES];
double average;
int max;
int min;
}students[MAX_STUDENTS];
char getLetterGrade(double grade);
void getData(Student &students)
{
ifstream fileIn;
int numStudents = 0;
fileIn.open(FILENAME.c_str());
if (fileIn.fail())
{
cout << "Could not open file" << endl;
system("PAUSE");
exit(1);
}
while (fileIn) {
for (int i = 0; i < MAX_STUDENTS; i++)
{
Student students;
getline(fileIn, students.name);
for (size_t i = 0; i < MAX_GRADES; i++)
{
fileIn >> students.grades[i];
}
return;
}
}
fileIn.close();
return;
}
void displayAverages(Student students) {
double total;
//double average;
int maxLength = 50;
cout << setprecision(1) << fixed << showpoint;
// Providing a header
cout << "\n\nGrade Averages\n";
cout << setw(maxLength + 1) << left << "Name" << setw(4) << right <<
"Average" << setw(6) << "Grade" << endl;
for (int i = 0; i < 22; i++)
{
cout << setw(maxLength + 1) << left << students.name;
total = 0;
for (int j = 0; j < MAX_GRADES; j++)
{
total += students.grades[i];
}
students.average = (double)total / MAX_GRADES;
cout << setw(7) << right << students.average << setw(6) <<
getLetterGrade(students.average) << endl;
}
}
char getLetterGrade(double grade) {
{
if (grade > 90) {
return 'A';
}
else if (grade > 80) {
return 'B';
}
else if (grade > 70) {
return 'C';
}
else if (grade > 60) {
return 'D';
}
else {
return 'F';
}
}
}
Let's take a look at your getData() function. It's defined as:
void getData(Student &students)
Since the return type is void, I'm guessing that you'll probably be passing in a Student and then modifying it in the function. However, you do:
Student students;
getline(fileIn, students.name);
Uh oh! This declares a new students that shadows the parameter students. So when you do students.name, you're talking about the local variable, not the parameter.
Kill that new declaration and things should work like you expected!
First thing - your code should have better structure!
void getData(Student &students)
{
ifstream fileIn;
int numStudents = 0;// you are not using this variable
fileIn.open(FILENAME.c_str());
if (fileIn.fail())
{
cout << "Could not open file" << endl;
system("PAUSE");
exit(1);
}
while (fileIn) {
for (int i = 0; i < MAX_STUDENTS; i++)//you should use auto here
{
Student students;//here you are making local object instead of changing the data off passed argument, you should get rid of this
getline(fileIn, students.name);
for (size_t i = 0; i < MAX_GRADES; i++)//you should use auto here
{
fileIn >> students.grades[i];
}
return;//you will return from you function after reading first student data so you should get rid of this
}
}
fileIn.close();
return;
}
After changes:
void getData(Student &students) {
ifstream fileIn;
fileIn.open(FILENAME.c_str());
if(fileIn.fail()) {
cout << "Could not open file" << endl;
system("PAUSE");
exit(1);
}
while(fileIn) {
for(auto i = 0; i < MAX_STUDENTS; i++) {
getline(fileIn, students.name);
for(auto i = 0; i < MAX_GRADES; i++)
fileIn >> students.grades[i];
}
}
fileIn.close();
}
This code should read a .pnm file. I tried to run it with 2 resolutions:
200x200 px: Here everything works just fine.
500x281 px: Code instantly crashes, raising a SIGSEGV error.
As far as I know, SIGSEGV is related to memory issues. I have 8GB of RAM, a quantity that I judge to be enough to run this. I don't have any idea about why it's happening and how to fix it.
Code
#define IO_ERROR (5)
#define X_DIMENSION (500)
#define Y_DIMENSION (281)
#define C_DIMENSION (3)
#define HEADER_SIZE (3)
#define BODY_SIZE (X_DIMENSION * Y_DIMENSION * C_DIMENSION)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int readImage(string fileName, string *imgHeader, string *imgBody)
{
ifstream inputFile(fileName);
if (!inputFile.is_open())
return IO_ERROR;
string line;
int count = 0;
while (getline(inputFile, line)) {
if (line.find("#") != string::npos)
continue;
if (count < 3)
imgHeader[count] = line;
else
imgBody[count - HEADER_SIZE] = line;
count++;
}
inputFile.close();
return 0;
}
void numericParser(const string *imgBody, unsigned char *numericBody)
{
int i = 0;
while(i < BODY_SIZE) {
numericBody[i] = (unsigned) atoi(imgBody[i].c_str());
i++;
}
}
void rgbParser(const string *imgBody, unsigned char rgbMatrix[X_DIMENSION][Y_DIMENSION][C_DIMENSION])
{
unsigned char numericBody[BODY_SIZE];
numericParser(imgBody, numericBody);
int k = 0;
for (int i = 0; i < X_DIMENSION; i++) {
for (int j = 0; j < Y_DIMENSION; j++) {
for (int c = 0; c < 3; c++) {
rgbMatrix[i][j][c] = numericBody[k];
k++;
}
}
}
}
void printInfo(const string *header, const string *body)
{
cout << "#-*- Image Header -*-" << endl;
for (int i = 0; i < HEADER_SIZE; i++) {
cout << header[i] << endl;
}
cout << "#-*- Image Body -*-";
for (int i = 0; i < 5 * C_DIMENSION; i++) {
if (i % 3 == 0) cout << endl << "R: " << body[i];
else if (i % 3 == 1) cout << " G: " << body[i];
else cout << " B: " << body[i];
}
cout << endl << ". . ." << endl;
}
int main()
{
string fileName;
cout << "File name: ";
cin >> fileName;
string imgHeader[HEADER_SIZE];
string imgBody[BODY_SIZE];
if(readImage(fileName, imgHeader, imgBody) == IO_ERROR)
return IO_ERROR;
// printInfo(imageData);
unsigned char rgbMatrix[X_DIMENSION][Y_DIMENSION][C_DIMENSION];
rgbParser(imgBody, rgbMatrix);
return 0;
}
Additional Info
I'm on Arch Linux 64-bit, using CLion as IDE.