C++ :: Reading Different Types Of Input Into Array
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
ADVERTISEMENT
Apr 4, 2015
I have a program to make a contact book. Included below i will post both header files and cpp files of my contact book, my contact class, and my address class. and my main.cpp. The reason address and contact are separate was because my teacher had us do an exercise where we used a header file of someone else's code, and didn't know what the functions actually implemented. But as the project has progressed he gave us the cpp for address.
Main.cpp
#include "Address.h"
#include "ContactBook.h"
#include "Contact.h"
[Code].....
View 9 Replies
View Related
Mar 28, 2015
How to properly read data from a .txt file.
If I have data stored in a .txt file, which is formatted/stored like this:
Code:
Apples and Strawberrys
10
Cherrys
12
Pears
16
Grapes, Melons, and Peaches
20
I know that if I read/extract, and print the data like this;
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream dataFile("test.txt");
string textData;
[Code] ....
Each line of data is stored in the string "textData" and printed to the screen, exactly as it was stored in the .txt file. So, all is clear to me up to that point.
But, what if I wanted to store each line of text in the string "textData", and store the numbers/integers into a separate variable called "numberData"? How would I retrieve and store the numbers (in the above example .txt file, every 2nd line) separately from the text?
For now, to keep things simple, let's assume that the data in the .txt file is stored/formatted as in my example (1 line of text, 1 line containing a number/integer, ...repeat) so, there is no need to test if the retrieved data is actually text or an integer, before it is stored in the appropriate variable type.
View 14 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
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
Mar 14, 2014
I want to create a function with a return type. However, I want to use it to read from a file (ifstream) and produce multiple different types of return types. The different file types returned would be always in the same order. For example
Text File:
Name
1
12
30
Area
I want to ifstream line 1 (Name) to an array of characters.
line 2, 3, and 4 to integers.
and line 5 as a string.
The basic problem is that if I make a function with one return type, it would only return one type of data to my int main(). I suppose I could create multiple functions that would run this for different variable types and destroy the invalid types. But this seems inefficient. It is being used to load in data from a previously saved file.
So far..
//function to load a player's data
string loaded(string fileName) {
ifstream loadfile;
loadfile.open(fileName);
[Code] .....
I was thinking I could possibly do something with a counter to count the lines and assign a value based on their order. The problem is with the 1 return value of a function. Maybe there is another operation I could use?
View 4 Replies
View Related
Feb 1, 2013
where to start and how it should be structured. how I should go about writing this program, like should i make functions, pointers, etc. And to display the menu, is it easiest to just use printf statements or is there something more efficient.
View 3 Replies
View Related
Apr 23, 2013
In class we were asked to create a C++ BTree program that would allow a user to input the following data types and then store said data in a .txt file:
0. ID 8 bytes
1. First name 30 char
2. Last Name 30 char
3. Street Address one 30 char
4. Street Adress two 30 char
5. City 30 char
6. State 20 char
7. Zip 10 char
8. Country 30 char
(I'm not particularly asking for full code, pseudo code would also be great). I had a great deal of my work done, unfortunately, the computer I was working on crashed, corrupting my files.
View 4 Replies
View Related
May 17, 2014
#include <iostream>
#include<fstream>
int decryption(int);
int multiply(int,int[][2]);
using namespace std;
main(){
int n;
ifstream inFile;
inFile.open ("out.txt");
[Code] .....
I was trying to store numbers read from a text file into 2D array but I am getting the error above.here is where the error occurs:
line 33: cout<<m[i][j]<<" ";
View 4 Replies
View Related
Jan 22, 2014
I have this
Code:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int check_up(char string[]);
int check_low(char string[]);
void to_up(char string[]);
void to_low(char string[]);
[Code] .....
When I compile this I have the following problems: warning: data definition has no type or storage class [enabled by default] in 'to_up(word)'conflicting types in 'to_up' function and to_low function warning: data definition has no type or storage class [enabled by default] into_up function error: unknown type name "word" in line 'printf("All uppercase %s. ", word):;'warning: parameter names (without types) in function declaration [enabled by default] in 'to_up(word)'and 'to_low(word)' 'note: previous declaration of "to_up" was here in function declaration of to_up function
View 7 Replies
View Related
May 1, 2014
How to do the problem below using loop?
Input numbers until the user types a 0, then output the product of the non 0 numbers: e
E.g., if the user types 2 4 6 0, the program should output 48
View 3 Replies
View Related
May 15, 2012
Code:
#include <stdio.h>
void f( char *a ) {
((int *)a)[1][1] = 8;
}
int main() {
int a[2][3] = {
[Code] ....
3.cpp: In function 'void f(char*)':
3.cpp:5: error: invalid types 'int[int]' for array subscript
View 3 Replies
View Related
Nov 22, 2013
The code is supposed to convert characters from an array into their respective ascii integers, and append a 0 if the number is less than 3 digits long. It then supposed to put it all together into one string.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char file[] = "This is a test";
char *ptr = file;
int length = strlen(file);
int i, numbers[length];
[Code] .....
View 4 Replies
View Related
Jan 18, 2015
I'm completely new to pointers and have a homework assignment due where I'm supposed to create a user defined dynamic array for player scores. The only errors I'm experiencing is a C2109: subscript requires pointer type and only on the lines that use the int *score; variable (57, 62, 64, 69, 71, and 82). I've already tried adding = nullptr to the variable and that didn't work.
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
void players(string[], int[], int);
void average(int[], int);
[Code] ....
View 3 Replies
View Related
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
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
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
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
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
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
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
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
Jan 18, 2014
I know to read a strings into array and tables.. what is they are mixed up?? strings are just names ( 3 characters) and there are bunch of table.. the max size was set to 60
ex. text file
JES
DAN
JEN
.
.
.
01010101
10010101
RAM
JET
01010010
10100101
.... and so on
should i use a while loop?
View 10 Replies
View Related
Jan 19, 2015
This is what I am supposed to be doing with this problem: Read the integer values in the file "numbers.dat" and display
* them in reverse order (that is, display the last number in the
* file first, the second-to-last second and so on). Use an array to
* store the values. It is safe to assume that the file contains no
* more than 100 values.
I have gotten far enough to read the file and display as an array, but it is displaying vertically rather than horizontally. So it is displaying 10 and a 1 on the first line then the 0 on the second line. Before I can work the reverse part out, I need it to display each number as 10 line 1 -235 line 2 so on and so forth.
This is my code so far:
void display_array_reversed() {
ifstream fin ("numbers.dat");
if (!fin){
cerr << "Error opening file" << endl;
exit(1);
[Code] .....
View 2 Replies
View Related
Apr 4, 2013
I have an assignment where I need to take an assigned image, rotate it 90 degrees to the right, flip it upside down, and turn it to grayscale; each with different outputs.
I was thinking if I can read in the image into a 2d array //array[height][width] or even just an array with the values of each pixel (r, g, b), I can just modify the array and spit it out when done.
if i can just get it into the array, I think I'll be fine. We've done similar assignments with modifying arrays for grayscale images and the other two I can probably figure out. But how am I going to read in each pixel into an array with three different values in each slot? This is my code so far:
Code:
#include<stdio.h>
View 1 Replies
View Related