C++ :: Read In Lines From A File / Store In Variables Then Construct Instances Of Class

Aug 22, 2013

I can't get my code to compile, i need to read in lines from a file and store them in variables. Then i have to construct instances of my class for how many lines there are in the file and take those variables into them.

I'm getting this error :

"a2.cpp:40: error: cannot convert `Employee' to `Employee*' in assignment"

#include<iostream>
#include<string>
#include<fstream>
void displayInfo();
using namespace std;
class Employee{

[Code] .....

View 1 Replies


ADVERTISEMENT

C++ :: Read From A File And Store Information In Separate Variables?

Dec 8, 2014

So I have to read from a file, and store the information in separate variables. My problem is that for some of the information I need multiple words, and for others I don't, so I cant simply store 1 word into a variable and store the next in another in so on. To demonstrate what I mean let me give an example.

Dog Cat Blue Bird Snake White Horse

I am able to store "Dog","Cat","Blue","Bird"...etc in variables but I don't know how to make it so I can store "Dog" in one variable, "Cat" in a second variable. and "Blue Bird" in a third variable. "Snake" would be the 4th and "White Horse" would be the fifth. How can I read a file and manipulate the pointer to grab what I need?

View 2 Replies View Related

C :: Read From And Store Values Of Each Line In Multiple Variables

May 6, 2013

So I have this text file that I am trying to read from and store the values of each line in multiple variables. Let's say my text file contains

AXSYZ3482 Tom 100 112 and my code below here works fine when the lines in the text file is separated by spaces.

Code:

while (fscanf(fp, "%s %s %d %d
", userID, name, &startLoc, &endLoc) != EOF) {
printf("%s %s %d %d
", userID, name, startLoc, endLoc);
}

But let's say my file was to look like this instead.

AXSYZ3482:Tom:100:112

But if i try this below...

Code:

while (fscanf(fp, "%s:%s:%d:%d
", userID, name, &startLoc, &endLoc) != EOF) {
printf("%s %s %d %d
", userID, name, startLoc, endLoc);
}

It seem to store the entire line in userID including the ":". I want to ignore the ":"'s and store everything in between in respective varibles in the order specified above.

So first string in userID, then ignore the :, then second string in name, and ignore the next :, and so forth.

View 4 Replies View Related

C++ :: Class Instances To Text File

Mar 29, 2015

Code:
#include<iostream>
#include<conio.h>
#include<string.h>
#include<ostream>
#include<fstream>
#include<iomanip>
using namespace std;
class MathProblem {

[Code] ...

So my program is quite simple, have the user answer the answer to a question, and then compare with the correct answer. I needed to implement an inheritance of MathProblem in my second class aswell.

The program runs as I intend but I wish to have the data forwarded to a text file. After that I must read back to the command prompt the text file contents. It's fairly easy for simple statements but I don't understand how to forward all my data from my classes.

View 3 Replies View Related

C :: Void Functions With If Else Construct Not Printing Multiple Lines

Apr 12, 2014

This code i made is a cent converter from 5 to 95 cents. The problem i'm receiving is when the 'cents' function is sent back to the 'main' function it only prints one line. It seems to just print the first if construct that complies with the statement. Is there anyway i can have this function print multiple cent values? For example if 60 cents was entered it would only print '50c', and i want it to print '50c' and '10c' instead.

Code:

#include <stdio.h>
int x;
void check(int x)
{
if( x < 5)
printf("Less then 5 cannot be calculated
");
else if(x > 95)

[code]....

View 3 Replies View Related

C/C++ :: Create Class Instances From Text File Stream

Nov 10, 2014

I am trying to do some exercises but am struggling at the moment. The first task was to create a class (Planet), then allow the user to edit their entry or view it.

The second one is to create class instances from a text file. The file contains a new planet on each line in the form: 'id x y z' where x/y/z are its coordinates. As the file can have more then one lines, it has to dynamically create an undefined amount of class instances.

To do this I used 'new' and it works ok - it prints each one out to the screen as you go so you can see it working. However... I'm trying to get into good habits here and am encapsulating the class which is where I am getting stuck. I can read from the class but cannot put the values from the file into the class.. ..using the member functions I have created anyway.

My code so far is:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Planet {
private:
int id=0;
float x_coord=0.0, y_coord=0.0, z_coord=0.0;
public:
int GetID(){return id;}

[code]....

If I change the SetID etc to just p->id, p->x_coord etc it works fine. But I'd rather find a way to do it that keeps the encapsulation. Using p->z_coord etc requires that you change the class variables from private to public.

The question I have been given is this:

Define and implement a function, generate planet, that takes a stream argument that has already been connected to a file (i.e. the argument is istream& fin). The function must create a new instance of planet using new and read its details from the next line in the file.Each line of the file is in the format id x y z.The function must return the newly created planet.

Also, how would you go about 'viewing' one specific class instance once they've been created? So say the file had 5 lines, line three was '4 6 2 6'. How would I go about viewing that planet afterwards? I don't think thats required but... I'm just wondering Although I'm also wondering, are we actually creating a new class instance for each line here? Or just destroying the previous one?

View 14 Replies View Related

C/C++ :: Why Cannot Use Variables Inside A Case In Switch Construct

Feb 26, 2015

If I have an integer variable like int a=9 then in the switch case If i write :

switch(a) {
case 4+a: printf("hii");
}

Then why is this statement a compile-time error that variables cannot be used inside a case statement why does the compiler not subtitutes the values in place of the variables.

View 1 Replies View Related

C++ :: Read Only Certain Lines From CSV File

Oct 21, 2014

I am trying to read lines from a CSV file and put the values into vectors. The CSV contains a large amount of data and I would like the program to only read certain lines from the CSV into the vectors. An example of the data is below. In my code the user inputs a secid. I would then like the code to only read lines from the csv where the secid matches what the user inputs. The secid's are also not in order so I can't just use a while loop.

My current code is below the data.

secideffect_datecusip ticker
10131014MAY19972313510 AMZN
10131008MAY19982313510 AMZN
10131028NOV20002313510 AMZN
10131023JUL20012313510 AMZN
10196601JAN199663858510 NB
10196601OCT199806605F10 BAC

ifstream infile3("filepath.csv");
if (!infile3) {
cerr << "Couldn't open file!"<<endl;
return 1;

[Code] .....

View 1 Replies View Related

C++ :: Read Certain Lines From A File?

Aug 22, 2014

I want to read certain lines from a file. Let say if the line contains word "makes", the line will be loaded on the screen.how to modify this code.

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;

[Code] ....

View 1 Replies View Related

C :: Compiling Sudoku Program - Declare Constant Instances As Global Variables

Sep 18, 2013

I am trying to compile a c program for sudoku. I have declare const instances as global variables, but when i try to compile the code it says that my declarations are not constant, here is some of the code.

#include <stdio.h>
#include <assert.h>

const int GRIDSIZE = 3;
const int GRID_SQUARED = GRIDSIZE * GRIDSIZE; //this line
const int ALL_VALUES = (1<<GRID_SQUARED)-1; //and this give//the error
int board [GRID_SQUARED][GRID_SQUARED];

View 3 Replies View Related

C++ :: Read And Rewrite Lines In File

Jan 2, 2015

My task is to write a function for borrowing books form library called borrow(name,surname, student_id, book_id), which will connect information of borrowed book's ID and student's information in the file.

File looks like this:

John Jackson 45 0
Michael Gregory 34 56
Ann Cawitch 23 0
Chris Lamb 34 50
...

First two words are name and surname of students. First number represents student's ID and second one represents ID of book. If ID of book is 0, it means that student hasn't borrowed book yet, so he can borrow some book. If ID of book is not 0, it means that student can't borrow a book until he returns the old one. So, when student borrows book, ID of book in file should change from zero to some number

This is my code:

void borrow(string name, string surname, int student_id,int book_id ) {
string a;
string b;
int c=0;
int d;
fstream f2;
f2.open("students.txt", ios::out |ios::in);

[Code] ....

Whenever I call function (for example borrow(John, Jackson,45,15)), I get answer "Student isn't sign up library."

Am I on a right path? How to make this code to work?

View 4 Replies View Related

C :: Segmentation Fault When Try To Read In The Lines Of File

Oct 21, 2013

I have to txt files, and want them to read into an array line by line and after then a split the lines with delimeters, at the first file I use ";", at the second file I want to split the lines to single words by spaces. My aim is to get the first word from the first file and compare with all the words from the other file, to check is there any matches.

My problem is, that at the first file works everything fine, and I want to do the with the second file, but after reading the lines into lines_input[], I check the an empty = null line, I get segmentation fault.

You can see the code below:

Code:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>

//Change the

[Code] .....

View 1 Replies View Related

C++ :: Read A File And Break It Into Lines And Characters?

Jan 31, 2014

I am writing a code to read a file and break it into lines and characters but I am getting an error do not know the reason for it though.

here is my code

#include<fstream>
#include<string>
#include<iostream>

[Code].....

The first line is executing properly but when it goes to the second line I get an error "the program has stopped working"

View 1 Replies View Related

C# :: How To Read Extra Lines Of Text File

Jan 12, 2014

private void open(object sender, EventArgs e) {
OpenFileDialog openDialog = new OpenFileDialog();
if (openDialog.ShowDialog() == DialogResult.OK) {
string[] lines = File.ReadAllLines(openDialog.FileName);
int k = 0;
while (k < lines.Length)

[Code] ....

So in this code, the user basically opens a text file from the dialog. Then the file gets read on all the lines and gets stored in an array. Now the reason I did this is because there are more fields of text boxes I have to fill in after I fill in the table (6x3). I'm getting an out of bounds error however.

19
19
19
0
95
0
0
0
0
5
0
0
5
1
0
0
51
1
110
Warrior

This is a sample text file that is being used. As you can see all the lines from start up until (excluding "110") will be used in the table. Now I want to read the last lines in this text file and put it in another text box. How do I do that?

View 2 Replies View Related

C++ :: Reading From File And Store Values Of Each Line In Multiple Variables

May 6, 2013

So I have this text file that I am trying to read from and store the values of each line in multiple variables.

Let's say my text file contains

AXSYZ3482 Tom 100 112

and my code below here works fine when the lines in the text file is separated by spaces.

while (fscanf(fp, "%s %s %d %d
", userID, name, &startLoc, &endLoc) != EOF) {
printf("%s %s %d %d
", userID, name, startLoc, endLoc);
}

But let's say my file was to look like this instead.

AXSYZ3482:Tom:100:112

But if i try this below...

while (fscanf(fp, "%s:%s:%d:%d
", userID, name, &startLoc, &endLoc) != EOF) {
printf("%s %s %d %d
", userID, name, startLoc, endLoc);
}

It seem to store the entire line in userID including the ":". I want to ignore the ":"'s and store everything in between in respective varibles in the order specified above.

So first string in userID, then ignore the :, then second string in name, and ignore the next :, and so forth. How I can accomplish this?

View 2 Replies View Related

C :: Program Count Number Of Lines Of Read Text File

Jan 25, 2013

I have a .txt file that contains, together with a few characters, columns of values that I want to save in different files like is written in the program (file 1, file2, file3 - a with x, b with y, c with z). The pattern of the source text file is like this:

known_vector frame1 151 65 0 frame2 151.000763 64.538582 0.563737
known_vector frame1 152 65 0 frame2 152.000702 64.542488 0.560822
known_vector frame1 153 65 0 frame2 153.000671 64.546150 0.558089

Until now I could manage to split the files, but the output gives me only zeros. First the program count the number of lines of the read text file, then it should display the desired columns of double values in three other .txt files.I've got for the three .txt files columns like this:

0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000

View 2 Replies View Related

C :: Read Characters From User Input And Construct A String

Mar 13, 2013

how to read characters from user and construct a sting and then extract part of it using substring?

View 2 Replies View Related

C++ :: Read Story From Input File / Separate Words And Output Lines On Which The Word Occurs

Feb 21, 2014

I have program that is supposed to read in a story from an input file and separate the words and output the lines on which the word occurs. It needs to read in another input file that has a list of words (1 per line) to ignore. i.e. skip them when running through the story. This is what I have so far, I've changed multiple things trying to get it running....

#include<iostream>
#include<fstream>
#include<map>
#include<set>
#include<vector>
#include<string>
#include"split.h"

[Code] .....

View 1 Replies View Related

C++ :: Read Variables From File

May 12, 2013

I want my program to read variables from file "input.txt":

Please choose the crystal lattice. Type 1 for BCC, 2 for FCC, and 3 for HCP: 2
Please enter lattice parameter (a): 1
For HCP please enter second lattice parameter (c): 1
Please enter the number of translated cells along X axis: 2
Please enter the number of translated cells along Y axis: 2
Please enter the number of translated cells along Z axis: 2

Moreover, I want program to read only the numbers after colon. How can I do it?

Code:
#include <iostream>#include <fstream> // to read from file
using namespace std;
int main () {
int latticeType;
int dimX, dimY, dimZ; // number of translated cells along each axis
float d; // lattice parameter

[Code] ....

View 2 Replies View Related

C :: Set Variables Based On File Read?

Dec 9, 2013

I am trying to fscanf a file and need to read the variable name 'f9' and 'a2' for example in the sample below and get their corresponding values '8' and '2' and then in my C program set the variables f9 and a2 accordingly (which I can declare as variables). Is this possible?

I making a Sudoku Solver and am thinking I would store the puzzle solution in a 2D array with a1=0, a2=1, etc... where 0, 1 are the array indices... then just print the array as the solution.

Note: The a1, a2, a3, etc... is my own numbering of the indices where the letter corresponds to the row and the number corresponds to the column (starting from 1) which I use when feeding to the solver.

I have a file that has the following format (output of Z3 SMT solver):

sat
(model
(define-fun f9 () Int
8)
(define-fun a2 () Int
2)
(define-fun c5 () Int
6)
)

View 1 Replies View Related

C :: Read Specific Values From A File And Store Them In Arrays

Apr 14, 2013

What i try to do is to write a code which reads some specific values from a file and stores them in an array. My File looks like this

Code:

XFOIL Version 6.96
Calculated polar for: Myfoil
1 1 Reynolds number fixed Mach number fixed
xtrf = 1.000 (top) 1.000 (bottom)
Mach = 0.000 Re = 0.200 e 6 Ncrit = 4.000
}

[code]...

View 11 Replies View Related

C++ :: Read Input File Of Data And Store Them In Arrays

Nov 5, 2013

Read the input file of data ( employees.txt) and store them in arrays. This company can have up to 55 employees [b]i need to do these following in these program:

Write a function to read the input file and store the data in arrays.
Write a function to calculate regular pay.
Write a function to calculate overtime pay
Write a function to calculate gross pay.
Write a function to bubble sort the employees into order by last name, first name.
Write any swap functions that are needed.
Write a function to write output to a file called payroll.txt

Format of file is EMPLOYEES.TXT[/b]

Hours Pay Rate Employee Number First Name Last name
40.0 10.00 A1234 Jane Adams
50.0 10.00 L8765 Mary Lincoln
25.5 10.85 W7654 Martha Washington
52.0 15.75 A9876 John Adams
45.0 25.00 W1235 George Washington
40.25 55.00 L9087 Abraham Lincoln
30.0 9.75 T9876 William Tell
42.5 12.50 M7654 Missy Muffett
30.0 10.00 P8765 Peter Piper

HERE IS MY CODE SO FAR:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void tellUser();

[code]....

View 9 Replies View Related

C++ :: Read A Text File And Store Contents Into 2D Array?

Aug 2, 2014

read a text file and store the file contents into a 2D array?

100 101 102 103 104 105
106 107 108 109 110 111
112 113 114 115 116 117
118 119 120 121 122 123
124 125 126 127 128 131

Here's my code:

const int ROWS = 5;
const int COLS = 6;
int array[ROWS][COLS];
ifstream inputFile;
inputFile.open("table.txt");

[code]....

When i run the program and try and display the array, it doesn't work.

View 1 Replies View Related

C++ :: Read / Write From A File Located Only In Virtual Store

Oct 16, 2013

Trying to read/write from a file located only in the virtual store. Having trouble loading the file in using

outputFile.open("%localappdata%virtualstoreProgram Files (x86)AGC est.txt");
and
outputFile.open("%localappdata%virtualstoreProgram Files (x86)AGC est.txt");

Although I can get it to work using:

inputFile.open("C:UsersNewlanceAppDataLocalVirtualStoreProgram Files (x86)AGCsettingsV5.cfg");

But I need it to work on other machines IE: Not Newlance.

View 2 Replies View Related

C/C++ :: How To Read And Store Large Amount Of Information From TXT File

Apr 1, 2015

Im trying to read and store several students information so that i can have an options menu where i can enter a student number and the program prints all the information stored about that student. The way i have it set up now, doesn't work for this because all info is reinitialized to stud1. Is there another way to store this info other than defining stud1, stud2,.....,stud200?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
struct student_info {
char first[20];
char last[20];

[Code]....

View 1 Replies View Related

C/C++ :: How To Read Information From A Text File And Store It Into Arrays

Mar 28, 2014

how I can read information from a text file into an array so afterwards I can display the array and it will show the contents of the text file?

the information inside my text files consist of names and numbers like so: "Collins,Bills 80" should I separate the numbers and names into two separate text files one for names and one for numbers?

My code so far is this:

#include <iostream> //for cin and cout
#include <fstream> //for input/output files
#include <conio.h> //for getch

[Code]....

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved