C/C++ :: Reading Unknown Number Of Integers In A Line?

Mar 1, 2015

I have a file with such a format:

string string 8 7 2
string string 7 12 19 -4
string string 1

As you can see I don't know how many numbers there are at the end. I have this code now:

while(!in.eof()) {
kr = 0;
getline(in, pavard, ','); getline(in, vard, ','); getline(in, gr, ',');
while(in.peek()) {
in >> k;
kr += k;
}
in.ignore();
}
in.close();

I tried various combinations with peek() function, but nothing worked.

View 2 Replies


ADVERTISEMENT

C++ :: Cin And Unknown Number Of Integers

Dec 2, 2012

i dont usually write console programs, and i cant seem to find out how one parses an unknown number of arguments with cin.

the program receives an unknown amount of integers in stdin, and i need to parse them withouth hanging.

Unfortunately, stdin is a async. stream and blocks, if it doesnt find any integers left.

I cant use peek() or seek() either, as both are async, too, (which makes me wonder what their exact use is?).

View 3 Replies View Related

C++ :: Reading From Console Multiple Strings Of Unknown Length In Combination With Integers

Oct 10, 2014

I want to read a string of unknown length from stdin. I tried to follow the approach from this link.

[URL]....

My code is like this:

Code:

#include <iostream>
#include <string>
using namespace std;
int n;
cin >> n;
cout << "The value of n is " << n << endl;
string str;
getline(cin, str);
cout << "The entereed string is " << str << endl;

What I have noticed is that if I take integer input from cin (cin >> n in the above code before getline, the control does not stop on getline to take str as input from the console. If I don't do (cin >> n) before getline then the control stops on getline and takes the string as input.

What is the best way to read from console multiple strings of unknown length in combination with the integers?

View 1 Replies View Related

C++ :: Reading Unknown Number Of Inputs And Adding Them In Vector

Jan 16, 2013

Consider the following piece of Code:

int ReadNumbers() {
int num;
vector<int> x;
cout << "Enter Numbers" << '

[Code] ....

The while loop is expected to terminate when the user provides an Invalid Input. But this while loop behaves unexpectedly when the user provides a 'Newline' input (by pressing Enter) and becomes an infinite loop. How can I prevent this from happening? Also I've heard that expecting invalid inputs isn't good code design. Is this true? If yes, then how can I solve my question without expecting Invalid Inputs?

View 10 Replies View Related

C/C++ :: Reading From A File Line By Line With No Specified Number Of Values

Apr 12, 2015

Im trying to read from a file, line by line, with no specified number of values in the file. Check out my code:

int main() {
string x;
ifstream fin;
int count = 0;
char ch;
fin.open("CWC_Master.txt");
if(!fin)

[Code] .....

Now, this works great! However, its skipping some lines. And I dont know why. For example: Lets say that the input file is:

superman toy
sm2335
19.99
batman toy
bm5532
25.99
aquaman toy
am6786
26.00

Where it should output the above, instead it outputs every other one. Like:

superman toy
19.99
batman toy
25.99
aquaman toy
26.00

How can I fix my code so that it SIMPLY(i say simply because I am still a beginner coder) can read line by line?

View 7 Replies View Related

C :: Reading Files Of Unknown Size

Jan 23, 2013

I want my program to be able to read text files in the format:

number number
number number
number number...

and so on, so there are two columns of values. The problem I'm having is, there are a lot of numbers in the file, and it can vary. The program needs to read the numbers, put one column into one array, and the other in another, perform some operations on it all, and eventually pop out two different arrays. In other words, after I've got these arrays, I don't need them for much longer. I was hoping I could dynamically allocated some arrays to store the numbers and just free them as soon as I'm done with them.

If the file wasn't of such variable size or if it was guaranteed to be under a certain number of variables, I would have used:

Code:

while ( fscanf(input, "%lf %lf
", &col_1[], &col_2[]) == 2 )
{
no_rows++;
}

The problem is I want to allocate "no_rows" as the array size, which I don't have until after I have read the file.

View 9 Replies View Related

C :: Reading A File Line By Line (invalid Write Of Size X)

Aug 17, 2014

I am trying to read a file line by line and then do something with the informations, so my method looks like this:

Code:
void open_file(char *link) {
FILE *file = fopen(link, "r");
if (file == NULL) {
fprintf(stderr, "Could not open file.
");
exit(EXIT_FAILURE);

[Code] ....

1) The first complain of valgrind is at the line where I use fgets and its telling me (invalid write of size x), but I have allocated my line to 56000 and the read line is shorter, why is there a write size error then :S?

2) at the line where I realloc where I try to shrink the space he's telling me: Address .... is 0 bytes inside a block of size 56000, But I know i need only this space so why is there a write over space error :S??

View 9 Replies View Related

C :: Reading A File Line By Line And Storing It Backwards Into A List

Sep 25, 2013

So I'm reading a file line by line and storing it backwards into a list. So if the file has has this format...
1
2
3
4

The code should store each line in a list as such...
4, 3, 2 ,1

Instead the code will store the last variable in all nodes. So the final list will look like this...
4, 4, 4, 4

Here is my code...

struct node *head = NULL;
int i;
while(read(in, &i, sizeof(int)) != 0) {
struct node *temp = malloc(sizeof(*temp));
temp->line = &i;
temp->next = head;
head = temp;
}

View 4 Replies View Related

C++ :: String Operation While Reading Line By Line From A File

May 12, 2014

I have to read the information about logic gates from a file. The file format is shown below:

Code:
gateOne = NAND(inpA, inpB)
gate2=NAND(1,2)
3 = NAND(23,25,26)

As, it can be seen from the above structure that whitespaces are not same everytime. So, to deal with this situation, i am using boost library to remove all whitespaces from the line which is being read and then try to find the name of gate and its input. My code is given below which is able to correctly find the gate names and its first input...but my code is not able to find the second, third and so on input names.

Code:
//C
#include <stdio.h>
//C++
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <boost/algorithm/string/erase.hpp>

[Code] ....

View 3 Replies View Related

C++ :: Passing Unknown Arguments To Unknown Function

May 1, 2013

So I'm making setTimeout and setInterval functions.

I have this remember function (that is part of Timing class) which takes a function pointer and a void pointer, which are remembered in that object.

Another (timing) function of that object is called in every loop of the program and when specific time passes that function calls the remembered function whit the remembered void pointer as argument.

The problem is that the functions that need to be called require unknown multiple parameters, so what I need to do is make a new class that will store the needed arguments. I make the function that needs to be called and that storage object and pass pointers to them to my remember function, when the remembered function is called it stores the data from storage object in new variables and dose it's thing.

View 3 Replies View Related

C :: Reading File Line By Line On Windows?

Oct 13, 2014

How to read a file line by line and then later access them by doing something like

Code:
lines[0] //Line number one
...
lines[100] //Line number one hundred and one
lines[100][0] //L
lines[100][1] //i
lines[100][2] //n
lines[100][3] //e
lines[100][4] //
lines[100][5] //n
...

View 13 Replies View Related

C/C++ :: Creating Program That Allows User To Enter Unknown Number Of Income / Expense Amounts

Feb 21, 2014

I'm trying to create a program that allows the user to enter an unknown number of income and expense amounts. The program has to us see a while loop and display the income total, expense total, and total profit or loss. I've got it really close, but I'm doing the loop wrong. It counts the sentinel value (-1) towards the total, instead of just ending like it is supposed to.

#include <iostream>
using namespace std;
int main() {
//declare variables
double incomeInput;
double expenseInput;
double incomeTotal=0;
double expenseTotal=0;

[code]....

View 5 Replies View Related

C++ :: Print 10 Integers Per Line?

Oct 28, 2014

The code I have is below. Im able to print them all onto one line but I need to print 10 per line separated by commas and Im not sure how to do that ):

for (int i = 0; i < MAXVALUE; i++){
for (int j = 0; j < counts[i]; j++){
output << i << ",";

View 1 Replies View Related

C++ :: Reading From File Line By Line?

Jul 27, 2013

First of all I need to know how to read from file.

Second, I need to read line by line from the file.

For example, the file contains:

8H 3S TH 9D 5S
5D KD 9H 4D 3D
8C TD 5D QS 2C

I wanna use array, so I need to store 8H in array[0], 3S in array[1], until 5S get stored in array[4].

I need to read the first line, the the second, and then the third.

View 3 Replies View Related

C/C++ :: Command Line Arguments With Integers?

Sep 10, 2014

I know this works as expected if your command line arguments are strings.

#include <stdio.h>
int main( int argc, char *argv[] ) {
printf("Program name %s
", argv[0]);
if( argc == 2 ) {
printf("The argument supplied is %s
", argv[1]);

[code]...

Would you be able to do something similar to this if you have integers or would you need to use the first method then convert it with atoi?

#include <stdio.h>
int main( int argc, int *argv[] ) {
printf("Program name %s
", argv[0]);
if( argc == 2 ) {

[code]...

View 4 Replies View Related

C/C++ :: Parsing Integers And Characters From A Single Line?

Mar 10, 2015

parsing integers and characters in same line.

I trying to parse g8,7.

{
token = strtok(NULL, ",");
token2 = strtok(NULL, "g");
}

I am trying to make 8 an integer so that i get two integers 8 and 7.

I already know how to get 7, but every time i do it i get (0,7) instead of (8,7).

View 8 Replies View Related

C :: Reading Integers That Are In A String?

Dec 3, 2013

I am doing a program and i need to read integers that are in a string and i dont know if i am doing it correctly.

Code:
unsigned int j=0, h1;
char num[100] , str1[100], str2[100], a[100], b[100];

sscanf(str1, "%[^;] ; %[^;] ; %d ", a, b, &h1); the string is : "0048915433190;24/10/2013;13h 7m 47s;13h 8m 59s;0;1;"

And what i want to read with sscanf is the hours, minutes and seconds.

View 1 Replies View Related

C++ :: Read Unknown Number Of Integer Values And Then Print Count Sum And Average Of Odd Values

Apr 9, 2014

write a c++ program that reads an unknown number of integer values and then print count, sum and average of odd values, even values, positive values, negative values!!

View 1 Replies View Related

C++ :: Reading Integers From File And Storing Into Array?

Oct 28, 2014

My main issue is that when I store just one value into a simple integer variable, it gives me a big negative number.

My goal is to store a file that reads like this into arrays:

2014 1 23
2012 2 15
2013 1 25
etc

Where I would have the first column in an array, the second in another and the third in third array since I am not allowed to use multidimensional.

View 5 Replies View Related

C/C++ :: Program That Keeps Reading Integers Until User Enters -1

Nov 21, 2014

This is the problem :- Write a program that keeps reading integers until user enters -1, then it prints the maximum and the minimum among all numbers (-1 should be ignored).

View 14 Replies View Related

C++ :: Reading Integers Out Of A Text File And Only Adds Positive Ones?

Sep 26, 2014

I'm basically trying to make a simple program that reads integers out of a text file and only adds the positive ones and not the negatives.

All is well except it won't take the last integer (the last line, I presume.) I took the negative out, nothing to do with that. I put more numbers in and I made the txt file less, no answer. No matter what the last number is, the program won't read it. I've been researching online and I've been seeing that it might be an issue with "while "!inFile.eof())".

Anyway here's the program:

#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inFile;
ofstream outFile;

[Code] .....

Here's the txt file:
5
6
-9
21
3

I'm always getting 32 for some reason.

View 2 Replies View Related

C :: Reading And Printing In A Specific Format Strings Of Characters And Integers

Feb 23, 2015

I have been skimming and searching but dont know how to word the search just right to find what I need to know. I have written simple stuff with the support of tutorials like weight conversion or loops counting up to a set number. Nothing amazing. The other day I found out how to input and print a string rather than a single text character which i though was bad ass. I even got it to read multiple strings on a single line and found a way to read multiple lines. I can even format it to read both integers and characters on the same line as long as I know the predefined format.

On to my question... How do I read multiple lines with both carecters and integers. for instance:

nissan 1996
toyota 1998
or more comples like
nissan gtr 1996
toyota markii 1998

I want to use
int year;
char make[10]; maybe need to use char make[10][10]; for an array i would guess.
char model[10]; optional for the extra data

but reproduce what i read in a different order. say...
1996 nissan
1998 toyota
vice the original format.

this is what I have tried.

Code: scanf("%s %s", &make,&year);

//The way I seen to read multiple lines was on here

scanf("%[^/t]", %make);

But this wont let me separate the two into two differnet definded data types. Let alone use printf to display them in reverse order.

View 1 Replies View Related

C++ :: Reading Certain Line Instead Of All?

Aug 24, 2013

how do i implement codes where it shall prompt for an user id.

From the input,the system shall look into the file..and output if the data exist.

For example in the file,

A123
Robinson
012345

B345
Crusoe
543210

So,i juz wanna input for B345.

Output shall be:

B345
Crusoe
543210

The code below is reading the whole data in the file but I only want to read one.

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
int main() {
fstream inFile;

[code]....

View 7 Replies View Related

C :: Reading Next Line In File

Mar 6, 2015

how to read the next line in the file. I think I need to use a for loop, but not sure how to implement it.Lets say my input.txt file has three names:

Sally
Tom
Sugar

I can get Sally, but not tom as yet. fgets also gets me to the same place.

Code:

int main(){
char str_1;
FILE * ifp = fopen("input.txt", "r");
fscanf(ifp, "%s", &str_1);
printf("%s", &str_1);
return 0;
}

View 1 Replies View Related

C++ :: Reading TXT Line Into A String?

Sep 11, 2014

void login() {
//username, password, option to register, option to exit, link to website
cout << "Would you like to login(1), or press anything else to register.?" << endl;
cin >> loginYorN;
if (loginYorN == 1) {
cout << "Please enter your username: ";
cin >> accountInfo[1];

[code]...

So as above, I ask them to input 1 for login, anything else for register. Whatever they input, it get's stored into loginYorN, then I check to see what they input. If they put '1' for input, then I ask them for their username, store that into accountInfo[1], and then asking for the password, storing that into accountInfo[2].

Now here is where I need to input line 1(username) from login.csv or login.txt and line2(password) and storage these into accountID and accountPW.

View 1 Replies View Related

C++ :: Reading The Rest Of Line

May 4, 2012

I had a problem which i fixed it but couldn't fix it in a nice way. Lets understand my problem first, i have a file and has data in the following format

Id(int) and Description(string)

Code:
1 this is first line
1 continuity of first line
1 again first line
2 this is second line
3 this is 3rd line
3 continuity of 3rd line
3 again 3rd line
3 and again
4 fourth line

I was only interested in the integer part and ignore the description at first, it was easy because they are separated by a tab.

Here is my code for that problem

Code:
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
#include<limits>
using namespace std;
int main(){
string str;

[Code] .....

But now i need that description again which i ignored at first means the rest of the line, so how to get it? so is there any way of solving it? Here is my approach but i can't find any method to access to the rest of line

Code:
value = atoi(str.c_str());
(if value==1){
cout <<restOfTheLine<<endl;
}

View 6 Replies View Related







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