Visual C++ :: Use For Loop To Read Certain Data From Txt File And Print To Console

Sep 28, 2014

I'm new to programming and i'm trying to do a certain task. I want to use a for loop to read certain data from a txt file and print them to a console. I'm trying to read student names and their grades.

Something like
3 // 3 represents the number of students.
George 97
Sarah 70
Maya 88

The data may vary but it's always in this format.

View 14 Replies


ADVERTISEMENT

C++ :: Read Comma Separated TXT File Into Array And Print It To Console

Oct 16, 2012

I have been trying to read a comma separated .txt file into an array and print it to console in C++. The txt file consists of two columns of double type data. For some reason the program runs, but gives blank output in the console. I want to know if I am doing something wrong. So far this is what I have:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
int i=0;
double x[10];
double y[10];
string line;

[Code] .....

View 3 Replies View Related

C :: Unable To Read Data From Text File And Print It

May 12, 2014

I am having trouble in reading data from my text file and print it out exactly like how it looks like in the text file. The problem im having is it will not read the last y Coordinates of the point. it keep reading the second last point for y coordinates which is wrong.

my text file is
0.0 0.0
0.0 1.0
1.0 0.0(but it read until here)
0.0 0.0(suppose to read the last point which is here)

For your information, this is my 1st year degree assignment in C programming. It is to create a program which can read text file (manually create) and print it out in a program and calculate the area for the polygon using ADT function ( .c and .h files)

*This is the code for my read file function*

Basically this accepts a Polygon and a file pointer as parameters. It reads the polygon point data from the file, pass the read data to plg_new() to create a new Polygon and returns the new Polygon created.

Code:

polygon *plg_read(polygon *New_polygon, FILE *Coord) {
int i;
int numberofvertices=0;
int count=0;
char filename[50];
double xCoor[50], yCoor[50];

[Code]....

This is the second function my polygon new code. This ADT function basically creates a new Polygon with malloc(), initialize all ADT data members with its parameter values and returns the Polygon.

Code:

polygon *plg_new(double *xCoordinates, double *yCoordinates, int numberOfVertices) {
int x;
polygon *New_poly = (polygon *)malloc(sizeof (polygon));
if(New_poly->xCoordinates == NULL || New_poly->yCoordinates == NULL) {
free(New_poly);

[Code]....

This is the rest of the code if you need to refer to other codings.

Code:
/**
*@file polygon.c
*@brief Functions for polygon / Struct has polygon numberOfVertices, polygon *xCoordinates and polygon *yCoordinates
*@author: Tan Xian Yao
*@id: 4323440
*@date: 22/04/2014
*/

[Code]...

View 6 Replies View Related

C :: How To Read Data In A File And Print It Then Copy Contents To A File

Mar 8, 2013

Code:

#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
char buffer[256];
FILE * myfile;
myfile = fopen("read.txt","r");

[Code]...

I also got an error in printing wherein the data in read.txt is "Hello" newline "World" and my program prints world twice.

View 3 Replies View Related

C++ :: Using Loop To Read In Data From A File?

Oct 30, 2013

My function is only reading the first line of numbers even though I have the loop to keep returning to it in main(). How do I have it read from the file until it runs out of data to retrieve?

File being read

90 83 94
72 65 81

File being sent the data(Only the second average is correct, working on the first)

Student Information
Student IDGradeAverageLetter Grade
1895612
90 83 94
289
90 83 94

#include <fstream>
#include <iostream>
using namespace std;
ifstream infile;
ofstream outfile;
void grades(int&,int&,int&); // Grades from the input file
int myavg(int,int,int); // Calculates the Average of the 3 grades

[Code] .....

View 8 Replies View Related

C :: Program To Read Binary Data And Print It - File I/O Error

Mar 14, 2013

Objective of this program is to read binary data from a file and print it.

Code:
#include <stdio.h>
void readFile(void);
int main(){
readFile();
return 0;

[Code] .....

But When I Run This Code First It Print "Error." Then Rest Of The File.Say In My File I Have "I AM HUMAN", It Prints "Error. HUMAN"!!

I Cant Figure Out Whats Wrong In The readFile() Function.It seems right to me.

View 4 Replies View Related

C++ :: Open A Text File Then Read Data And Print It Back?

Nov 5, 2013

I am in an "intro" C++ course and am having trouble with a section of my current project.

We are required to open a text file, read the data from that file, and print it back.

However, we are supposed to let the user input the name of the file that is to be opened. This is the code I have so far. It is not opening anything.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int NUM_QUESTIONS = 30;
typedef char CharArray[NUM_QUESTIONS];
string fileName;

[Code]...

View 2 Replies View Related

C++ :: Read Data From File Then Make It Into Array Of Structures And Print Afterwards

May 13, 2012

I am trying to read from a data file that has input as :

12.0, 11, 123
14.0, 12.1, 3

And I want the program to read the data from the file and then make it into an array of structures and then print that array afterwards.

Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_INPUT 1000
int n =0;
int i = 0;
typedef struct {
double x[MAX_INPUT];

[Code] .....

The program when run gives the following output:

Ishtiaque-Mohammed-Khans-MacBook-Pro:Comp20005 IshtiaqueMKhan$ gcc -Wall -ansi -o ProjectB ProjectB.c
ProjectB.c: In function "main":
ProjectB.c:59: error: incompatible type for argument 1 of "print_array"

View 1 Replies View Related

Visual C++ :: Employee Payroll - Read Data From Text File And Sort

Nov 11, 2013

i tring to write a employee payroll code with functions and read data from the txt. file and also bubble sort that sorts and displays starting from employees last name and calculations.

//txt file//
Hours Pay rate empID 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

and here is my code

Code:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int SZ = 55;
void tellUser();

[Code] .....

here are the errors the are giving

Code:
payroll.cpp: In function "int bubbleSort()":
payroll.cpp:52:8: error: "lastpos" was not declared in this scope
payroll.cpp:52:18: error: "numemp" was not declared in this scope
payroll.cpp:56:10: error: "swapmade" was not declared in this scope
payroll.cpp:57:16: error: "i" was not declared in this scope

[Code] ....

View 14 Replies View Related

C++ :: Print To Console Contents Of A File Using Foreground Colors And Other Attributes

Jun 29, 2013

Suppose I have a txt file that I want the contents printed to the console in such a way that every five words are colored blue and the following five are red. How do I accomplish such a task? I am currently only able to print the contents of the file in regular color using ifstream.

View 1 Replies View Related

C++ :: How To Read From A File Then Display To Console

Mar 21, 2013

Write a program that reads from total.dat file and displays its contents to the console. The program should allow the user to add new entries to the file.

So far this is what I've got and I'm not sure if I even started right.

#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream dataIn;
ofstream dataOut;
dataIn.open("total.dat");
cout << dataIn;
return 0;
}

View 1 Replies View Related

C++ :: Loop To Read In Data And Check Data

Oct 8, 2013

struct receivers
{string fname, lname, team,;
int receptions, yards, TDs, longest,rec20, fumbles, yac, firstdown;
double, averagepergain, averageperrec
}

View 2 Replies View Related

C++ :: Read File With Comma And Bar Separators Output In Console

Jan 13, 2013

I have a file named "A6.txt" inside it has this code:

April Joe, 1, SUPER DUPER ULTRA SECRET, 02031982|
Matthews Jocob, 2, TOP SECRET, 11031992|
Garfield Kitty, 3, SECRET, 04041942|
Lake Bill, 4, MEH, 12031968|
Jones Betty, 5, WATCHLIST, 06031974|
Goodman Betty, 6, BANE OF SOCIETY, 05021952|

Very Simple, all it has is "Name, ID, Access, Date of Birth" (DOB needs to be formatted like 00/00/0000 if possible)

Output should Look like :

Client #1:
Name: April Joe
Access: SUPER DUPER ULTRA SECRET
DoB: 02/03/1982

View 4 Replies View Related

C++ ::  Saving PNG File In Console Program / Losing Data?

Jul 18, 2013

I am working on a project that uses a web service to extract a PNG image from a web server and save it on a local machine. It's done via a system call. I can get the output of the system call to a FILE but it appears to be losing data, most notably the PNG chunk names (IHDR/PLTE/IDAT/IEND, notably). (Unfortunately piping the output to an exterior file does not work through a system call.) The end result is ~6KB smaller than the file I am intending to retrieve.

This is the code I'm using (adapted from other material I found via searches here):

ofstream png;
png.open(/*outputfile*/, ios::binary);
FILE* fp = _popen(/*SYSTEMCALL*/, "rb");
string responseString;
if(fp != NULL) {
char a[100];
while(!feof(fp)) {
fgets(a, 100, fp);
responseString += a;
}
}
_pclose(fp);
png << responseString;

Am I missing something or just doing it wrong?

View 11 Replies View Related

C/C++ :: Display Data From File Into 2D Array - Print Closest Bmi From Data

Mar 28, 2015

#include <iostream>
#include <fstream>
using namespace std;
int main() {
int r = 0;
int c = 0;
int num[17][15] = { 0 };
[Code] ...

// Here is my code for displaying the data from the text file into a 2d array and height next to it, but I am not able to diaplay the height from 60 to 76 next to the row of the 2d array, as shown in the table below. This is my program:

Recently the health authorities have listed a new way to calculate the body mass index (BMI) of an individual.
Values of 20 – 24 are classified as normal, 25-29 as overweight, and 30-34 as heavy.

The table below is a portion of a typical BMI table.

BMI:202122232425262728293031323334
Height:
60102108113118123128133138143149154159164169174
61106111116122127132138143148153159164169175180
.
.
.

MY task is to write a program that does the following things:

1.Produce the table by reading in the data from bmi.txt (on Moodle) into a 2-D array.

2. Display the table neatly on the screen, with row and column headings. The BMI should go from 20 to 34. The height should go from 60 to 76 inches.

3. Prompt the use for their height (in inches) and their weight.

4. Make the program print the closest BMI.

If their weight is lower than the values on the table, use 20 as the BMI.
If their weight is higher than the values on the table, use 34 as the BMI.

5.Loop the program so the user can find more BMI’s.

View 7 Replies View Related

Visual C++ :: Pythagorean Triples - Read Integer N And Print

Sep 27, 2014

I need to create a code in c fits the description below,

Description : A positive integer triple (a, b, c) with 0 < a < b < c and a^2 + b^2 = c^2 is called a Pythagorean triple. Write a program in C which reads an integer N and prints

1. the total number of Pythagorean triples (a, b, c) with c < N,
2. every such Pythagorean triple, and
3. the triple that has the largest value of c.

Hint: you can enumerate all possible pairs (a, b) with 0 < a < b < N and check if they satisfy a

2+b
2 < N2
.

Example Input/Output

- Enter a positive integer: [3]
There is no Pythagorean triple in this range.

- Enter a positive integer: [15]
There are 3 Pythagorean triples in this range:
(3, 4, 5)
(5, 12, 13)
(6, 8, 10)
The triple with the largest value of c is (5, 12, 13).

- Enter a positive integer: [6]
There are 1 Pythagorean triples in this range:
(3, 4, 5)
The triple with the largest value of c is (3, 4, 5).

View 1 Replies View Related

C :: Read And Print To File?

Jul 15, 2013

Ok, I think I got the main idea of the I/O chapter. Though I think I might be oversimplifying the program because I am getting an interesting error.

Goal here is to read numbers from a file and print the numbers with totals for two of the columns and a calculated average for of the totals.

NOTE: The tables for the info from file look a lot better on my end. [QUOTE] seems to mess it up a bit from copying and pasting.

Info from problem:

Car No. Miles Driven Gallons Used
----------------------------------------------------
54 250 19
62 525 38
71 123 6
85 1,322 86
97 235 14

carinfo.txt file:

54 250 19
62 525 38
71 123 6
85 1322 86
97 235 14

program:
Code: updated code

View 8 Replies View Related

Visual C++ :: Print Sequence Data Structure

Sep 15, 2012

Printing out a sequence data structure. I am using a PC running Windows 7 with Microsoft Visual Studio 2008. This sequence has a dynamic array which stores a value_type which can be any of the built in data types for C++. Initially the sequence is 30 slots long. When the program starts the user starts at the beginning of the sequence by choosing the "!" option from the menu. The user will then insert a number such as 11 by selecting the "I" option (upper or lower case). Then the user will go to the next position in the sequence by selecting the "+" option then insert 22, then go to the next position and insert 33.

To show that the numbers are actually in the sequence the user goes back to the beginning of the array by selecting "!" and then select "C" to display the current number of 11. Then the user goes to the next position by selecting "+" and then "C" to display 22 and so forth. At this point the member function called current() works just find , but when trying to print the contents of the entire sequence the program displays garbage. Why?

Code:
// FILE: sequence_test.cpp
// An interactive test program for the new sequence class
#include <cctype> // Provides toupper
#include <iostream> // Provides cout and cin
#include <cstdlib> // Provides EXIT_SUCCESS
#include "sequence2.h" // With value_type defined as double
using namespace std;
using namespace CISP430_A2;

[Code].....

View 2 Replies View Related

C :: Any Way To Read Entire File And Print It On Screen

Feb 26, 2013

is there anyway to read an entire file and print in on screen? file consists of strings and integers!

View 7 Replies View Related

C/C++ :: Read From File And Print Output To File?

Apr 20, 2014

I know this is how you read from a file. I also want to print my output to the same name of the file plus ".txt". So if my file is called "text" I want to print it to a file called "text.txt".

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

[Code]....

Would it be something like this? Is it better to use "a" or "w" in the fopen? I want to write to it several times.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

[Code].....

View 2 Replies View Related

C :: Read Txt File And Print Contents Numbering Each New Line Of Text?

Apr 25, 2013

I have written the following code but i am stuck. Write a program that will prompt the user for a file name and open that file for reading. Print out all the information in the file, numbering each new line of text.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
int main()
{
char line[81], filename[21], c;
int i = 1;
FILE *inFile;

[code]....

View 3 Replies View Related

C++ :: Read List Of Numbers From A File And Then Print Out In Reverse Order

Apr 6, 2013

1. Construct a class diagram that can be used to represent food items. A type of food can be classified as basic or prepared. Basic food items can be further classified as meat, fruit, veg or Grain. The services provide by the class should be the ability to enter data for new food, to change data for food and to display existing data about food.

using this class definition write a main program to include a simple menu that offers the following choices:

1. Add food
2. Modify Food
3. Delete Food
4. Exit this menu

2. Read a list of numbers from a file and then print them out in reverse order. State whether the list is palindromic or not.

View 2 Replies View Related

C++ :: Read A File With 2 Numbers In It - Nested For Loop

Mar 17, 2013

I am attempting to read a file with 2 numbers in it. The first indicates the number of rows the second, the number of columns. So for a file (Data.txt) that contains the numbers 5 7, I want to display

0 0 0 0 0 0 0
1 1 1 1 1 1 1
0 0 0 0 0 0 0
1 1 1 1 1 1 1
0 0 0 0 0 0 0

and write that output to a file.I can display the correct number of rows and columns but I can't figure out how to display alternating rows of 0's and 1's.

#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inFile;//declare file

[code]....

View 5 Replies View Related

C/C++ :: Print Output With Append Data In TXT File

Oct 22, 2014

I was recently introduced to the fstream header file. I want to know is their a easy way to print an output with append data in a .txt file.

And secondly I am also having an error with my header file. It says error: cannot open source file "fstring" when I hover my mouse over "#Include<fstring>".

View 7 Replies View Related

C/C++ :: Program That Uses While Loop To Read A Set Of Integers From Input File?

Apr 20, 2014

The question is "Write a program that uses a while loop to read a set of integers from an input file called "data.txt" and prints the total number of even numbers read from the file."

Code below is the program I have created so far. I keep getting the window to pop up but not stay up so I am assuming I am doing something wrong.

#include <iostream>
#include <fstream>
using namespace std;
int main() {
std::fstream input("data.txt");
if (!input.is_open()) {
std::cout << "There was an error opening data.txt";

[Code]...

View 5 Replies View Related

Visual C++ :: MFC GUI-setting COMPort And Read Hex Data From Pic (ASCII)

May 22, 2014

I want to setup my MFC-based dialogue "IDD_LOADCELL" with serial communication so that when that dialogue is pop up, the data from PIC (external device) which is in hex file format is display on the edit control box on the dialogue. The variable name of the edit control box is "m_loadP "and "m_loadT1". type of variable for both are string. I want to get the code for the COMPort setting and reading the data from pic and finally display it on the edit control box created on the dialogue.

View 11 Replies View Related







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