C :: Reading Input Without Using Arrays - File I/O
Sep 23, 2013
I am having trouble with file I/O.
Let say I have an input file like this
x1 y2 b4
85 z4 x2
I'm trying to read the input without using arrays. I would use fscanf(*arguments*) to get the first char, which is x. Then I call it again to get the second one, which is 1. Now I would need to get the second input, y2. How do I skip the white space character to get to char y ? And is it an effective way to get all inputs though ? (each input, example x1, is read into a function).
View 3 Replies
ADVERTISEMENT
Feb 16, 2014
I am trying to read from my input file Transactions.data which is set up as follows
V 250190
A 250190 2008 4 8788.00 0.00 000
and so on and so forth.
I am trying to read my V then go to switch (decision) to choose my case and preform further functions. My problem is that i cannot get past the first line so if it was
V 213400
V 123456
My code is supposed to check that the ID (213400) is valid then go on to the next line but it wont do that and prints out the first ID 6 times. My code is as follows:
char descision;
ifstream input;
input.open("Transaction.data");
if (input.fail()) {cout << "File could not be opened." << endl;}
input >> decision;
[Code] ....
View 1 Replies
View Related
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
Apr 24, 2014
I'm reading input from a file using an ifstream. The program doesn't seem to read any characters but continually returns eof no matter what file I use. Even when I do ifstream.open(filename) nothing changes. This program is intended to be the lexical analyzer of a compiler, so symbols are returned to indicate what kind of input is in the file.
Here's the code:
#include "lex.h"
#include <cstlib>
#include <iostream>
#include <fstream>
//other functions
char Lex::getChar(){
ifstream input;
[Code] ....
I'm sure I messed up the initialization of the ifstream somehow.
View 1 Replies
View Related
Feb 16, 2015
I need to read input from a file , which contains multiple sentences of varying lengths. After each new line char, i need to store that sentence into an array.
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
[Code].....
View 2 Replies
View Related
Oct 1, 2014
The program will prompt the user to enter a every student's first and last name, major ( of 4 choices CMPS MATH PHYSICS or EECE ), and grade. it's agreed that the professor will enter the name NoMore to stop the entry.
the program willl then store the info in a txt file. after closing the input file, the program will open it again read the records one at a time until end of file, and do some statistics including: highest grade, average grade, and highest average by major.
Finally the program will print the results....
View 6 Replies
View Related
May 29, 2014
I've been experimenting with char arrays and getting user input through different methods.
int main() {
char userInput[21];
/*I understand that over here, a maximum of 20 letters can be input, and only letters before a space will be stored in userInput*/
std::cin >> userInput;
std::cout << userInput << std::endl;
[Code] ....
As I was testing, whenever I would input a single word for userInput (for example "hi"), the program would work as expected: it would output "hi" and I'd be able to input a sentence of sorts for userInput2 (for example "hello world") and have it outputted.
But if I were to input more than one word for user Input (for example "hi how are you"), the program would output "hi" as expected, but it wouldn't let me input anything for userInput2 and would just output the rest of the first input; in this case, "how are you" would be outputted and the program would end. I am not aware of the logic error at play.
View 7 Replies
View Related
Jul 18, 2013
I have not completed this program yet, but I have already run into a snag. For the Input Validation part, every time you enter a number regardless if it is positive it will still display the cout statement: "Please enter positive values". The program runs correctly where I currently am at, but it just keeps reading that statement even if a user enters a positive value. I have run the debugger, but it really is not showing me anything other than I notice it jumping to that statement after every value that is entered.
Here is my code, once again I have not finished this yet, but I would really like to get this fixed first before I continue. I will keep debugging in the meantime...
Code:
// A local zoo wants to keep track of how many pounds of food each of its three monkeys eats each day during a typical week. Write a program that stores this information in a two-dimensional 3 X 7 array, where each row represents a different monkey and each column represents a different day of the week. The program should first have the user input the data for each monkey. Then it should create a report that includes the following information:
// Average amount of food eaten per day by the whole family of monkeys.
// The least amount of food eaten during the week by any one monkey.
// The greatest amount of food eaten during the week by any one monkey.
// Input validation: Do not accept negative numbers for pounds of food eaten.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double totalAverage = 0; // variable to store the total average, initalized to zero
[Code] ......
View 11 Replies
View Related
Feb 7, 2014
I'm trying to create a function where it allows the user to type in multiple amounts of integers, so if the user wanted to have 3 different storages that hold different integers, the input would look something like this:
5
97 12 31 2 1 //let's say this is held in variable "a"
1 3 284 3 8 // "b"
2 3 482 3 4 // "c"
2 3 4 2 3 // "d"
99 0 2 3 42 // "e"
Since we don't know what number the user will input every time, I'm not sure how to create a dynamically allocated array that will create an x amount of arrays every time.. I want to be able to access each index of a, b, c, d, e or however many arrays there are.
So far, this is what I have, but I'm having trouble creating the arrays since it's unpredictable. I'm purposely not using vectors because I don't really get how pointers work so I'm trying to play around with it.
int* x;
int length, numbers;
cin >> length;
x = new int[length]
for (int i=0;i<length;i++)
{
cin >> numbers; //this doesn't work because it only grabs the first line for some reason
x[i] = numbers
}
View 3 Replies
View Related
Mar 18, 2015
I have an HTTP/1.0 webserver that I'm building in C that needs to exhibit CGI script functionality. Specifically, what I'm having trouble with is that I have two processes: process 1 is the webserver, process 2 is the CGI script. I fork in the webserver, and then call exec to run the CGI script (specifically, I used execv()). I've read from the CGI specifications in the RFC that in the case of a webserver receiving a POST request, the CGI script should read the arguments for the POST request from stdin. However, when I write to stdin, it simply echoes what was written to the terminal window and when the CGI script tries to read from stdin, it blocks.
So, with all that said, I'm pretty sure there is some simple conceptual explanation to my problem, but I can't figure it out. Do I need to use pipes or some form of interprocess communication to send the data from the webserver to the CGI script, or can it just be done with stdin (and possible stdout)?
View 2 Replies
View Related
Jun 30, 2012
Let's say I have a list of words that are followed by a number.
Word 1
Word 2
Word 3
Word 4
I need two arrays. One for the words and one for the numbers.
Code:
string STORE_ARRAY[100];// stores string (word)
string operation[STORE_ARRAY];
int PARA_ARRAY[50];//stores parameter (#)
int parameter[PARA_ARRRAY];
For the file input, I'm using this:
Code:
in.is_open()){
string line;
while(getstring filename;
cin>>filename;
vector<string> file_input;
[Code] ....
So first it gets saved in file_input, but then it needs to split it and move it into the appropriate array.
View 2 Replies
View Related
Mar 6, 2015
I have seen functions that declare arrays in the function input even thou the arrays is already declared in main. Why do you do this?
For example:
int ova(int antal, char glosorSv[][MAX], char glosorEn[][MAX])
int main(void)
char glosorSv[][MAX]
char glosorEn[][MAX]
View 2 Replies
View Related
Jul 15, 2014
I'm still fairly new to c++ and am having trouble with dynamic arrays. My task is to create an array for test scores. The user needs to define the size of the array and then input data to later sort and calculate the average.
Below is what I have so far. I am struggling with the user input part. I receive the following errors for the data input of the individual scores:
"error C2108:subscript is not of integral type"
"IntelliSense:expression must have integral or unscoped enum type"
Code :
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
//user input total number of scores
cout<<"Please enter the total number of test scores."<<endl<<endl;
[Code] ....
View 1 Replies
View Related
Mar 7, 2013
Okay so this is part of a small uni module around the arduino board. The task is to simulate traffic lights at a junction. At the moment my code below runs through the sequence with 2 seperate lights. When one set of lights turns RED , the other turns GREEN.In my main loop method I need to add a button for a crossing. I want to be able to check if the button is pressed at each of the delays but do not want to have to hold it down.I have thought about putting it at the top of the loop but then the program will only check if button is pressed in that point therefore the user will have to hold it down?
Code:
int green1 = 13 ;
int amber1 = 12 ;
int red1 = 11 ;
int green2 = 6 ;
int amber2 = 7 ;
int red2 = 8 ;
}
[code]....
View 5 Replies
View Related
Apr 28, 2014
I wanted to know if there was a way to separate the inputs received if there were a few keyboards and mice hooked up to a desktop/laptop.
Would there be a particular class that would best be utilized to do this?
I read that MouseEventArgs is one class that can recognize events, but I'm not sure if its best for what I need.
View 2 Replies
View Related
Oct 15, 2014
I'm in a CIS 143 class which is Introduction to Programming using C++. We've been assigned a lab with little to no detail on how to do the things listed.
The purpose of this lab is to practice the file input and output concepts. For this lab you may assume that the input files exist and contain appropriate data inside of them.
Four separate text files* which contain the following lines, different for each file.
(*: This isn't on the lab, but don't worry about what the name, age, id, and major are. those can be random and I can just change them what they need to be)
Name
Age
Student ID Number
Major
Your program should ask the user which of the four input files they would like to open, and then read the contents from the file. Once the contents have been read in, it should output to both the screen and a file named "output.txt" the four values were read in, arranged as follows:
"Hello <name>! You are a <age> years old <major> student, and your ID number is <ID number>"
Now I am by no means saying "Do it for me". I just need to know how to have a user type file1, file2, file3, or file4, and then have the program open that file, then have the output.txt file arrange the wording into the sentence above.
This is what I have so far and I don't think it is anywhere near correct:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inFile;
string file1;
string file2;
[code]....
View 1 Replies
View Related
Nov 3, 2012
The following program takes user input into two arrays and should then determine how many items are different by comparing them with a loop. The comparison always show zero correct answers.
#include <iostream>
using namespace std;
const int QUESTION = 20;
//class for testing grades
class TestGrader {
[Code] ....
View 4 Replies
View Related
Apr 21, 2013
I need reading a specific input syntax.
something like
<Input> :: R C <Content>
3
3
.3. .15 9.2
.1. ..6 ...
6.5 7.. ...
... .5. ..9
4.7 .8. 1.6
9.. .6. ...
... ..1 7.4
... 6.. .9.
5.1 49. .3.
View 2 Replies
View Related
Nov 25, 2013
I want the numbers to be put into the array until it hits a new line. Maximum number of numbers is 1000. What can I replace that while loop with to stop it from reading further into the input file?
Code:
while (input != '
'){
for(i=0; i<1000; i++)
fscanf(ifp, "%lf", &auctions_prices[i]);
View 2 Replies
View Related
Nov 26, 2013
Program background: Ticket Sales Details You will sell tickets in advance and at the door. Prices for buying in advance and at the door will be given. Also, the total number of tickets sold in advance will be given. Each guest will have a unique number. If there are n tickets sold in advance, then these guests will be numbered 0 through n-1. As the event starts, requests to buy tickets at the door may be made and these guests will be numbered sequentially, starting at the lowest unassigned number. The maximum number of guests will be 1000. The first line of the file contains the following three values, separated by spaces: Cost of the presales tickets (in dollars), Cost of the tickets at the door (in dollars), and the number of presale tickets. The first two values will be positive real numbers to two decimal places and the last will be a positive integer.
The second line of the file will contain one positive integer representing the number of auction items followed by a positive real value to two decimal places (at most) representing the minimum bid increment, in dollars. The first value is guaranteed to be 1000 or less and the second will be in between 1 and 50, inclusive.
The third line of the file will contain all the prices of the auction items, in dollars, separated by spaces, in order. Thus, the first price is the price of item 0, the next price is the price of item 1, and so on. These values will be real numbers represented to up to 2 decimal places.
The fourth line of the file will contain the three following positive integers pertaining to the raffle: the number of raffle tickets available, the cost of a raffle ticket in dollars, and the number of raffle prizes. (It's strange to have raffle tickets that don't cost a whole number of dollars.)
The fifth line of the file will contain each of the values of the raffle items, in dollars, separated by spaces, in order. Thus, the first price is the price if item 0, the next price is the price of item 1, and so on. These values will be real numbers with upto 2 decimal places.
The sixth line of the file will contain ten positive integers representing the number of each of the drinks 0 through 9, in order, that are in stock.
The seventh line of the file will contain ten positive real numbers with upto 2 decimal places representing the price of each of the drinks 0 through 9, in order.
The eighth line of the file will contain a single positive integer, numEvents, representing the number of events that occur at the charity ball. These events are split into two groups: actions by guests at the ball and awards given (raffle, auction, person, totalrevenue). All of the actions precede all of the awards. You will produce exactly one line of output for each event described. Here are the formats of each event that could occur:
If a patron buys a ticket at the door, a command will be on a line by itself:
BUY TICKET k
where k is a positive integer indicating the number of tickets bought at the door. These guests will be numbered as previously mentioned. You are guaranteed that the total number of tickets bought, including presales, will not exceed 1000. This is what I have so far and I cannot figure out why it wont calculate the total revenue. I am not completely sure if it is even accessing the if statement in main.
Code:
#include <stdio.h> #include <stdlib.h>
#include <string.h>
#define N 1000
[Code].....
View 4 Replies
View Related
Mar 11, 2014
I have to read 2 arrays, put them in a file. Then make a 2nd program, read the data from the file and show the 2 arrays. I believe I have fully completed the first program, but I am still working on the 2nd one. On the 2nd one I am trying to create a function that displays the data from the file.
//first part
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int num_stores = 3;
const int col = 4;
const int size=4;
void showData( const string[], const int[][col], int size );
[code]....
View 3 Replies
View Related
Nov 7, 2014
I am reading a file then printing the data onto the other file. It is working, however when I check to see if each variable is being properly set after reading the file a issue arises.
Example of the file being read
Code:
Vehicle PV50CAN passed camera 1 at 05:33:26.
Vehicle W867BRO passed camera 1 at 05:33:29.
Vehicle KQ63ARU passed camera 1 at 05:33:38.
Vehicle K954ITQ passed camera 1 at 05:33:40.
Vehicle V220MXB passed camera 1 at 05:33:42.
[Code] .....
View 14 Replies
View Related
Mar 31, 2015
#include <stdio.h> /* fopen, fclose */
#include <stdlib.h> /* exit function */
#include <string.h> /* string library */
#define STRSIZENAME1 41 /* length of name */
#define STRSIZENAME2 100 /* amount of names */
#define STRSIZEAGE 100 /* amount of ages */
/* Name: main */
int main(int argc, char* argv[]) {
[Xode] ....
This is the code I have so far. The goal of the assignment is to sort two parallel arrays, one with names and another with their ages, at the same time to alphabetize them. I am stuck with trying to pull the data from the file itself and concatinating it. I have searched ways to do so and this is the best I understood. So the error is it will get me the first person's name, but it won't get the age and move to the next line in the file. It just keeps repeating the same thing and I can't figure out why.
I attached the sample file with names and ages to view the format of the text file itself.
PS the other defined variables are used later for sorting the data. right now I am just trying to get the data itself and put it in an array but between my book and the internet I can't seem to store the data properly. That is also why I am printing it in the while loop as well so I can see what my arrays look like but I will take that out too once I know the data is good
Attached File(s)
peoplefile.txt (157bytes)
View 2 Replies
View Related
Jun 3, 2013
This is the first time I'm working with 2d arrays and I have to read the pixels in the input file, then store the pixel from the input image file into the array. And there is a maximum width and height of 500. If the file's too big, then I have to print an error message. Also, my array should only be filled up to the dimensions of the input image file.
I have the code for everything up to that point but I'm not too sure how to approach this. I've defined max_width and height at the top of my file as 500 and I'm thinking of putting this part of the program into a new helper function. But I'm not completely sure how to start, I do know that the array would look something like this though:
int array[max_height][max_width].
1) the syntax of printing the error message/storing the values and 2) the values for my pixels in the input file are all in a single column; each value after the ppm format header is on a new line.
what the input file looks like:
Code:
P3
493 401
255
71
0
0
76
4
5
87
11
NOTE: First three lines are the header information for the ppm format; the numbers that follow go like this: red, green, blue, red, green, blue, etc.
View 1 Replies
View Related
Nov 3, 2014
I have trouble reading data from a text file to parallel arrays. the text file (album.txt) has this format:
Code:
(song1 title)
(song1 artist)
(time1)
(song2 title)
(song2 artist)
(time2)
.
.
etc
I want to store these data into three arrays:
title [], artist[], time[] this is what I have so far:
Code:
#include <stdio.h>
#include <string.h>
#define SIZE 9999
int main ()
{
FILE *tFile;
char title[SIZE];
[Code]...
the problem is that each array reads the whole text? I want to store artists name in char artist[] .. titles in char title[] and so on
View 5 Replies
View Related
Oct 21, 2014
im new to programming and new to C, just started with arrays and im somewhat stucked, where i have a .txt file which contains items and prices. I want the items and the prices split into 2 different arrays
The .txt looks like that:
orange 0.89
banana 0.79
apple 0.59
I now want to split the items in a char items[100][100] and float prices[100]. how to split this, preferably with fscanf ?
View 7 Replies
View Related