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


ADVERTISEMENT

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 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 :: How To Save Returned Of Unknown Length

Sep 15, 2014

I call a function that returns a string, and I can print it out fine, but I want to test the result of the function to see if it returns 0. But I can't just call the function again (GetNextToken(b)) because it will generate a different token. I can't allocate space for the string because I'm not sure what the size of the returned string is going to be.

Basically I want to see if the GetNextToken(b) returns 0, and if it doesn't then print the string. And running GetNextToken(b) again will give a different result.

Code:
int main(int argc, char **argv) {
SomeStruct* b = CreateStruct(argv[1],argv[2]);
printf("HERE %s", GetNextToken(b));

View 3 Replies View Related

C++ ::  split String (unknown Length)

Mar 27, 2013

I manage to split this str = "abc,def,123"

to s1 = "abc", s2 = "def", s3 = "123" with this piece of code using find and substr.

string str, s1, s2, s3;
getline(cin, str);
unsigned pos1 = str.find(",");

[Code] ....

But what should I do if the len of the string is unknown ?

For example, str = "abc,def,123,ghi,jkl,456,mno" and so on...

View 4 Replies View Related

C/C++ :: How To Define String Of Unknown Length

Mar 7, 2014

how to define a string of undefined length in c programming?

View 1 Replies View Related

C++ :: Read A String Of Unknown Length From Stdin

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:

#include <iostream>
#include <string>
using namespace std;
int n;
cin >> n;
cout << "The value of n is " << n << endl;
}

[code]......

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 5 Replies View Related

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++ :: Read All Values / Strings Form Unknown File

Nov 4, 2014

For an assignment I have to write a program which basically converts 8 bit binary numbers to ASCII and outputs the assembled text. Here's the catch:

The 8-bit binary numbers are provided by some external file (which only contains 8 bit binary numbers); the name and hence length is not known. The external file is called with a pointer upon execution

(./"conversion program" < external_file.in).

I'm getting the 8 bits as a string, calculate/convert decimals, output char type. HOW do I know when to stop the loop? If I just pick an insanely high number I get random stuff at the end; no boundaries obviousely lead to an infinite loop. Can I determine the lenght of this random ext file somehow nonetheless?

Is it possible to create a vector which dynamically adjusts itself until there are no more strings = end of the file?

View 6 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 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++ :: Reads In Two Positive Integers That Are 20 Or Fewer Digits In Length - Output Sum?

May 1, 2013

Write a C++ program that reads in two positive integers that are 20 or fewer digits in length and outputs the sum of the two numbers.

Your program will read the digits as values of type char so that the number 1234 is read as four characters '1', '2', '3' and '4'. After they are read into the program, the characters are changed to values of type int. The digits will be read into a partially filled array and you might find it useful to reverse the order of the elements in the array after array is filled with data from the keyboard.

Your program will perform the addition by implementing the usual pencil and paper addition algorithm. The result of the addition is stored in an array of size 20 and the result is written to screen. if the result of the addition is an integer with more than maximum number of digits(that is more than 20 digits) then your program should issue a message saying that it has encountered "integer overflow".

You should be able to change the maximum length of the integers by changing only one globally defined constant. Include the loop that allows the user to continue to do more additions until the user says the program should end.

For some reason the sum won't add or output though, This is what i have so far:

#include <iostream>
using namespace std;
const int MAXIMUM_DIGITS = 20;
void input_Large_Int (int a[], int& size_of_A); //input function for the two big integers
void output_Large_Int(int a[], int size_of_A); //output function for the two big integers and the sum integer
void add(int a[], int size_of_A, int b[], int size_of_B, int sum[], int & size_Sum); //add function for the big integers' sum

[Code] .....

View 1 Replies View Related

C++ :: Read Two Positive Integers That Are 20 Or Fewer Digits In Length And Output Sum Of Two Numbers

Apr 30, 2013

so basically my project goes like this:

Write a C++ program that reads in two positive integers that are 20 or fewer digits in length and outputs the sum of the two numbers.

Your program will read the digits as values of type char so that the number 1234 is read as four characters '1', '2', '3' and '4'. After they are read into the program, the characters are changed to values of type int. The digits will be read into a partially filled array and you might find it useful to reverse the order of the elements in the array after array is filled with data from the keyboard.

Your program will perform the addition by implementing the usual pencil and paper addition algorithm. The result of the addition is stored in an array of size 20 and the result is written to screen. if the result of the addition is an integer with more than maximum number of digits(that is more than 20 digits) then your program should issue a message saying that it has encountered "integer overflow".

You should be able to change the maximum length of the integers by changing only one globally defined constant. Include the loop that allows the user to continue to do more additions until the user says the program should end. What I have so far is

#include <iostream>
#include <cstdlib>
using namespace std;
void reverseArr(int a[], int liu);
void addLargeInt(int a1[], int liu1, int a2[], int liu2, int sum[], int& liu_sum);
int main() {
cin.get(next);

[Code]...

View 2 Replies View Related

C++ :: Dynamic Array Of Variable-length Strings

Oct 10, 2013

I believe that everything is fine except that I can think of the condition can be inserted inside the parentheses ..

#include <iostream>
using namespace std;
int main() {
int i=0, k=0;
char *string_n, **matrix, temp;

[code].....

View 2 Replies View Related

C++ :: Generate Random Strings From A To Z With A Length Of 3 For Each Generation

Sep 16, 2014

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;
string randstr(int);
int main()

[Code]...

I want to generate random strings from A to Z with a specific length of 3 for each generation. For example:

BRC
YUG
YFH

How do I fufill this with the code presented and the function at the bottom?

View 2 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 Sharp :: Add Multiple Records In Console Application?

Nov 22, 2012

how l can add multiple records when using the console app. This is the excercise l have done so far:

string name;
string surname;
int score;           
Console.Write("Enter Name:");

[Code]....

l want to be able to add more student records and display the student with the top score.

View 4 Replies View Related

C++ :: Link The Strings To The Integers?

Nov 8, 2014

Code:
#include <iostream>
#include <string>
using namespace std;
int main()

[Code] ......

How do I link the strings to the integers?

View 4 Replies View Related

C++ :: Vector With Strings And Integers

Apr 5, 2013

I have a file where the first column shows letters, second column shows numbers.

How could I make it to print everything exactly how it is in the file – a vector/array with strings and integers?

View 13 Replies View Related

C++ :: Adding Strings Of Integers

Jan 29, 2013

I'm trying to write an algorithm for a larger project that will take two strings which are both large integers (only using 10 digit numbers for the sake of this demo) and add them together to produce a final string that accurately represents the sum of the two original strings. I realize there are potentially better ways to have gone about this from the beginning but I am supposed to specifically use strings of large integers as opposed to a long integer.

My thinking was to take the two original strings, reverse them so their ones position, tens position, and so on all line up properly for adding. Then one position at a time, convert the characters from the strings to single integers and add them together and then use that sum as the ones position or otherwise for the final string, which once completed will also be reversed back to the correct order of characters.

Where I'm running into trouble I think is in preparing for the event in which the two integers from the corresponding positions in their strings add to a sum greater than 9, and I would then have carry over some remainder to the next position. For example, if I had 7 and 5 in my ones positions that would add to 12, so I would keep the 2 and add 1 to the tens position once it looped back around for the tens position operation.

I'm not getting results that are in any way accurate and after spending a large amount of time stumbling over myself trying to rectify my algorithm, I am not sure what I need to do to fix this.

#include <iostream>
#include <cstdlib>
#include <string>

[Code]....

View 6 Replies View Related

C :: Read TXT File With Strings And Integers

Nov 15, 2013

While I execute the fileprint function i was able to retrieve the record from the txt file. but at the end of the console im getting randoms number i have tried to understand what causing the problem. I have attached a screenshot....

Code:

void fileprint(){ Code: int c;
struct student{
long id;
char name[20];
int mid1;

[Code] .....

View 7 Replies View Related

C/C++ :: Parsing Integers Values From Strings

Sep 30, 2014

So I have this assignment to read a file in, malloc some arrays, run it through a perceptron and to display the final weights. I have the majority of it already written but this is only my third program in C and I'm more familiar with Java and Python than C.

The problem I'm having is when I read in command line arguments, I can't seem to parse integer values from the strings in argv[i] by using atoi().

I've included the piece of code where I'm trying to 'parse.' I understand atoi convers ascii to integer, but I don't understand if it just gives you the ascii code or the number that it actually represents. I attempt to use atoi on lines 33-35

The input arguments in the command line are:

bob in.csv 100 5 10

int main(int args, char* argv[]){
int ** ra; // array of array of pointers
FILE *ifp; // file pointer
char cc; // char var for reading input from file
int i = 0; // counter

[Code] ....

View 3 Replies View Related

C :: Read Text File With Strings And Integers

Nov 16, 2013

i have prepared a code the read from txt file with values such integers and strings. but the code i have prepared reads only 1 line. how can i make the code to read multiple records from txt file.

input records example in txt file:
9009 James 90

Code:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int c;
struct student{
}

[code]....

View 4 Replies View Related

C++ :: Getline With Integers / Double / Strings And Delimiters?

Apr 22, 2013

I want to use getline to read stuff from a file that looks like:

Student ID:88152; Grades: 83.67, 92.45, 74.43, 97.95, 76.45; Name:Abercrombie, Neil
Student ID:92485; Grades: 77.65, 92.31, 60.47, 67.12, 99.64; Name:Aderholt, Robert
Student ID:10623; Grades: 37.95, 83.11, 64.46, 74.76, 95.30; Name:Alexander, Rodney
Student ID:76793; Grades: 53.13, 81.02, 83.71, 90.75, 88.92; Name:Baca, Joe

I have to print to the screen the id numbers, the grades and the names

I'm using getline but im having trouble print out the grades

It gets and displays everything except the grades. One of the grades only gets displayed

It should be a while loop, but im just working with the first line for now

ifstream inf;
char fileName[ MAX_STR_LEN ] = "ex.txt";
char testStr[ MAX_STR_LEN ];
int testInt;
double testDouble;

[Code] ....

View 3 Replies View Related

C++ :: Mapping Strings To Integers - How To Print String Zero

Dec 12, 2013

I'm very very new to maps and am really just trying to hash them out by myself.

If you're mapping strings to integers:

map <string, int> myMap;
myMap[ "zero" ] = 0;
myMap[ "one" ] = 1;

How do I print the string "zero", for instance, from myMap?

cout << myMap.at(0) << endl;

doesn't work. Nor does:

cout << static_cast<string>( myMap.at(0) ) << endl;

I need access to the string using the int and the int using the string. Or just direct access to one or the other. . . It's just confusing that they're technically mapped to one another but I can't really access either of them.

View 4 Replies View Related

C :: Convert From Strings To Integers And Pass Into Linked List

Feb 11, 2013

I have a problem set where i have to read in numbers from a file as strings, convert from strings to integers, and pass the integers into a linked list, where each integer is a node. This is what I have so far:

Code:
# include <stdio.h>
# include <stdlib.h>
# define MAX_INT_SIZE 10000
typedef struct integer BigInt;
struct integer {

[Code] ......

View 10 Replies View Related







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