C++ :: Sort Data From Text File Into Array As Its Being Read?

Feb 12, 2013

I am suppose to make a program that reads in data from a text file (integers only) and sorts them as it inserts them into an array of size 10. I did this using an insertion sort, which worked great. But now I am being told that I need the function has to read ALL of the numbers in the text file, not just the first 10, and I am not allowed to store them THEN sort, it has to be sorted as being stored.

This is what I have for sorting first 10

void sortArray(int iArray[])
{
string fileName = "";
fstream inFile;
int tmp = 0;

[Code]....

View 3 Replies


ADVERTISEMENT

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++ :: Not Able To Read Data From Text File And Convert It To Integer

Mar 25, 2013

I am trying to read an array values from Tmin.txt file. I take array size from user viz. number of raw & column and then, read the following data from Tmin.txt file which look like this:

20 40 20 25 30

20 30 40 20 25

20 40 20 25 30

20 30 40 20 25

30 40 40 30 40

I am using getline, and then converting it to int by using atoi. But my code is not working.

Code :
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

[Code] ....

View 10 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++ :: 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 :: How To Read A Text File Into Array Of Strings

Mar 3, 2013

In a program, I have a text file (called MyDictionary.txt) which has thousands of words in alphabetical order. I need to make a C program that reads in this text file and then makes an array called char Words[# of total words in the text file][length of longest word].

Aarhus
Aaron
Ababa
aback
abaft
abandon

View 6 Replies View Related

C++ :: How To Read Information From Text File To Array

Apr 4, 2014

Perhaps my original wording on this was confusing so I will ask a different way.

if I have a text file called

data.txt

and it has the following in it.

12345
67890
12345
67890

how would i read this information into an array called

int data[4][5]

This is in C.

View 10 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++ :: How To Read Data From TXT File Into Array

Feb 25, 2013

I wanna read a txt file like this
1.00518 2.01903 3.01139 4.01343 5.02751 5.99913 7.00011 7.99851 .....
and fill the array P[i][j] with the data.

I try to use

if (myfile.is_open()) {
//for (int i = 0; i < 1; i++)
//{
getline (myfile,DMArray[0]);
cout << DMArray[i] << endl;
//}
myfile.close();
}

But this read the data into a string. How should i do with this string. Besides I know the dimension of the data I am reading.

View 1 Replies View Related

C++ :: Read A File Into 2D Array And Display Data

Nov 15, 2014

I am currently working on a problem where I read a file into a 2D array, and display data. If you saw my code earlier, this is the same problem, but I am now further along.

The file (90 characters) looks like this:
S
R
R
S
R
R
R
R
S
S
....

The "R" resembles rainy days, the "S" resembles sunny days, and the "C" resembles cloudy days. There are 90 characters in the file. The first 30 are for the month of June, the next 30 are for the month of July, and the last 30 are for the month of August. The 31st day of the months are NOT included.

I read the data into a 3 x 30 array, where the row indicates the month (0 = June, 1 = July, 2 = August) and the column indicates the number of days in each month.

Now I need to create a function that creates a report and displays, for each month AND for the whole 3 month period:
1) The number of rainy days
2) The number of cloudy days
3) The number of sunny days
4) Which of the 3 months had the largest number of rainy days

Here is my code so far:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MONTHS = 3;//Number of months

[Code] .....

There are no compiling errors, but I am not getting the data I want. My rCount and cCount variables end up being 0, and my sCount variable is 90.

View 4 Replies View Related

C++ :: Read From A File And Place Data Into Array

May 3, 2014

Assume you want to read from a file and place the data from the file into an array. The array can hold 10 integers. The file may contain any number of integers with 1 integer per line. Fill in the missing loop test condition so that we will read from the file until either end of file occurs or the array is full. Note we are using cin to read from the file since input redirection is being used.

cin >> temp;
arrayIndex = 0;
while ( _____________________________________) {
array[arrayIndex] = temp;
arrayIndex++;
cin >> temp;
}

View 2 Replies View Related

C++ :: Read Byte Of Char Data And Convert It Into Text String Of Binary Data That Represents Hex Value

Dec 26, 2013

I am writing a program where I need to read a byte of char data and convert it into a text string of binary data that represents the hex value...

i.e. The char byte is 0x42 so I need a string that has 01000010 in it. I've written the following subroutine....

------------- My Subroutine ----------------------------------------------------------------------
void charbytetostring(char input, char *output){
int i, remainder;
char BASE=0x2;
int DIGITS=8;
char digitsArray[3] = "01";

[Code] ....

When I submitted the byte 0x42 to the subroutine, the subroutine returned to the output variable 01000010... Life is good.

The next byte that came in was 0x91. When I submit this to the subroutine I get garbage out.

I am using a debugger and stepped through the subroutine a line at a time. When I feed it 0x42 I get what I expect for all variables at all points in the execution.

When I submit 0x91 When the line remainder = input % BASE; gets executed the remainder variable gets set to 0xFFFF (I expected 1). Also, when the next line gets executed..

input = input / BASE; I get C9 where I expected to get 48.

My question is, are there data limits on what can be used with the mod (%) operator? Or am I doing something more fundamentally incorrect?

View 6 Replies View Related

C++ :: Unable To Read Data File From Two Dimensional Array

Apr 27, 2014

I am new to C++ and I am having an issue reading in a 2 Dimensional array from a data file. I am very close to reading it in perfectly except for one issue, the loop is ignoring the first value from the data file.

This is the code I have so far:

#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <functional>
#include <conio.h>
#include <iomanip>
#include <string>
#include <fstream>

[Code]...

This is the values from the data file:

300 450 500 210
510 600 750 400
627 100 420 430
530 621 730 530
200 050 058 200
100 082 920 290

When the Program shows my array it comes up as:

450 500 210 510
600 750 400 627
100 420 430 530
621 730 530 200
050 058 200 100
082 920 290 0

Every array location is moved up one, like the program just completely ignored the 300 at the beginning.

View 1 Replies View Related

C++ :: Saving Array Data Into Text File

Dec 4, 2013

I have a program that saves all information temporarily into memory (into the array), however, I need a permanent save solution. I have the full program working correctly, formatted perfectly, but it's missing the file array to file output.

Essentially, whenever I'm presented with the menu I'll be able to add entries, search by last name, show the entire list, and exit and save. Whenever I select option 4 the program should save to a file "address_book.txt". Whenever I reload the program, it should load from "address_book.txt" and populate the array with the preexisting data.

My question is focused on how and where I should implement the file output. What I have thus far:

#include <iostream>
#include <string.h> //Required to use string compare
#include <fstream> //Eventually used to store array into file
using namespace std;
class AddBook{

[Code] ....

View 5 Replies View Related

C/C++ :: Loading Data From Text File Into Array

Jan 25, 2015

I am currently working on a project that requires me to "load the data in the file into array at the beginning of the program."

I have a text file with data, and I need to populate an array with the information. From then on, I am supposed to be able to add, display, and search that array. However, I can't figure out how to add the data from the file into an array. I was trying to find out how to search the text file itself. So it threw me off balance and I've been staring so long at the screen I can't really focus.

View 7 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

C++ :: Read In A TXT File And Sort It By SSN In Ascending Order

Jan 25, 2014

So I need to read in a .txt file and sort it by SSN in ascending order. How to start this but here's what I have so far:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Information {
char name[25];
long SSN;

[Code] .....

Also here is the contents of the .txt file:

Matthews,Jocob
459237148
19
3930 4th Blvd, Yourcity, NJ 88710
......
and so on.

View 1 Replies View Related

C++ :: Getline - Get Text File And Read Only First 100 Lines Of Text Content

May 31, 2013

I am trying to get text file and read only first 100 lines of the text content using c/c++. how can i do it?

is it something like string line;
getline(stream,line);

??

View 7 Replies View Related

C/C++ :: Array To Sort Strings In Alphabetic Order And Read From Stdin

Mar 20, 2014

I have a pre-declared array which sorts strings to it's alphabetic order and want to change it so it reads from stdin.

char *array[] = {"aaa", "ccc", "bbb", "ddd"}

I tried doing something like this:

for (i = 0; i < length; i++)
scanf("%s", &array[i]);

I just can't bring it to work. Another thing is, the input is a a bunch of strings separated by commas and ends with a period. Since I have to make a working C model which gets translated to assembly language later on I can't use functions like strtok.

View 6 Replies View Related

C++ :: Sort Text File By First Number Of The Line

Dec 24, 2013

I have a text file like this

0 1 2 C
10 1 2 S
7 1 2 C
11 1 2 S
9 3 43 C
10 3 43 S
1 3 43 C
101 3 43 S

with this code

ifstream in("fout2.txt");
if (in) {
vector<string> lines;
string line;
while (getline(in, line))

[Code] ....

I obtain this result

0 1 2 C
1 3 43 C
10 1 2 S
10 3 43 S
11 1 2 S
101 3 43 S
7 1 2 C
9 3 43 C

but i want a result like this :

0 1 2 C
1 3 43 C
7 1 2 C
9 3 43 C
10 1 2 S
10 3 43 S
11 1 2 S
101 3 43 S

View 3 Replies View Related

C/C++ :: Sort Word Length From A Text File

Mar 23, 2014

I was asked to create a program to find word frequencies, word length in a text file. I am able to use map to do the frequencies part but don't know how to sort the the output accordingly to the length of the words. The example output that I should get is

1 a. 362
i. 157

2 an. 122
at. 201

3 add. 2
age. 1

int main(void) {
static const char* file = "demo.txt";
map<string, unsigned int> wcount; {
ifstream fileStream(file);

[Code] .....

View 10 Replies View Related

C/C++ :: Read Integers Into Array / Sort Numbers And Print Out Occurrence Of Each Number

Apr 6, 2014

My C programming class wants us to write a program to read integers into an array, sort the numbers and print out the occurrence of each number. I've tried everything that I can think of but the core dump is still occurring.

void countValues ( FILE *inf, int list[], int size ); /* Function prototypes. */
void printFrequencies( const int list[], int size );
int main (void) {
int status = EXIT_SUCCESS; /* defaulting status to success. */
FILE *inf = fopen( "numbers.txt", "r" ); /* input data file */

[Code] .....

View 6 Replies View Related

C++ :: How To Bubble Sort Data From A File

Sep 26, 2014

I'm trying to write a program that will print the data of 10 records from a file and then, sort them descending to ascending by year.

Here are the records:

NameYearTuition
David20111582.38
Sylvester2012728.82
Ben19920
Brandon1995500.25
Riley1997845.19
Mark2009700.05
Roberts2002450.87
Butler2005920.78
Steve20001000.00
Joe19941200.15

Here is my code so far:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;

[Code] ....

How do I go about arranging it?

View 1 Replies View Related

C/C++ :: How To Bubble Sort Data From A File

Sep 26, 2014

I'm trying to write a program that will print the data of 10 records from a file and then, sort them descending to ascending by year.

Here are the records:

NameYearTuition
David20111582.38
Sylvester2012728.82

[Code].....

View 6 Replies View Related

C++ :: Data Input Into A Text File While Not Deleting Original Data

Apr 19, 2013

I want to input data into text file while not deleting the original data in the file and I use something as

ofstream writefile;
writefile.open("example1.txt");
if (writefile.is_open()) {
for(j=0; j<N; j++) {

[Code] ....

But this will delete the original data.

View 3 Replies View Related

C :: Create File To Write Text And Read Back The File Content

Mar 15, 2013

The Objective Of This Program Is To Create A File To Write Text And Read Back The File Content. To Do That I Have Made Two Function writeFile() To Write And readFile() To Read.The readFile() function works just fine but writeFile() doesn't.

How writeFile() function Works? when writeFile() function Execute It Takes Characters User Type And When Hit Enter(ASC|| 10) It Ask "More?(Y/N)" That Means What User Want? Want To Go Next Line Or End Input?

If "Y" Than Inputs Are Taken From Next Line Else Input Ends.

But The Problem Is When Program Encounters ch==10 It Shows "More?(Y/N)" And Takes Input In cmd variable.If cmd=='Y' I Mean More From Next Line Than It Should Execute Scanf Again To Take ch I Mean User Input.But Its Not!!! Its Always Showing "More?(Y/N)" Again And Again Like A Loop.

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

[Code].....

View 5 Replies View Related







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