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


ADVERTISEMENT

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++ :: 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/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 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/C++ :: Printing A Map That Contains A String And A Class With Integers?

Dec 14, 2014

I'm new to c++, so how to print the contents of a map that contains a string and a class of integers for option 1. what formatting should I use?

#include <iomanip>
#include <vector>
#include <sstream>
#include <map>
#include "IPHost.h"
#include "EncryptedConnection.h"

[Code] .....

View 6 Replies View Related

C :: How To Take String Of Numbers And Turn It Into Array Of Integers

Mar 2, 2014

I know strings are essentially just arrays of characters, so what would be the easiest way to take each individual digit and put it into a separate space in an array?

ex.) *str = "90210"

array[0] = 9
array[1] = 0
array[2] = 2
array[3] = 1
array[4] = 0

All my attempts at achieving this just result in an array full of garbage numbers. What I've done is

Code:
int *array;
array = malloc(sizeof(int)*(strlen(str));
for(i=0; i<strlen(str); i++) {
array[i] = str[i]
}

also, I should mention that the string will be defined in main, and its converted into an array in a separate function.

View 2 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 :: Turning String Of Numbers Into Array Of Individual Integers

Mar 2, 2014

In my program, I am fed a string that contains integers such as Code: *str = "45678" and my program is simply supposed to read the number in and store each given number in a separate spot in an integer array. So basically, when my program has finished running it should be stored like:

Code:

arr[0] = 4
arr[1] = 5
arr[2] = 6
arr[3] = 7
arr[4] = 8

The way I have been attempting to do this was just copying it to the array like so:

Code:

arr = malloc(sizeof(int) *(strlen(str));
for (i=0; i<strlen(str); i++) {
a->digits[i] = str[i];
}

however, this just seems to return an impossibly high garbage value when I do. I'm assuming the way I'm trying to store it is 'illegal', but I cant seem to find online a proper way to do it.

View 2 Replies View Related

C/C++ :: Conversion Of Negative Integers To String Without Using Atoi Function

Sep 21, 2014

char intToStr(int a) {
int n, i, j, sign, set;
char r[10], s[10];
if (a[0] == '-')
sign = -1;
if (sign == -1)

[Code] ....

I have doubt at the time of handling of negative numbers at the time of converting to string ....

View 2 Replies View Related

C++ :: Read Set Of Integers Then Find And Print Sum Of Even And Odd Integers

Apr 21, 2014

I'm a beginner at c++ and I need to write a program that reads a set of integers and then finds and prints the sum of the even and odd integers. The program cannot tell the user how many integers to enter. I need to have separate totals for the even and odd numbers. what would I need to use so that I can read whatever number of values the user inputs and get the sum of even and odd?

View 2 Replies View Related

C :: Reading Infinite String?

Sep 24, 2013

I have made a program that can take an infinite string:

Code:

#include<stdio.h>
int main(void) {
int i = 2, j = 0;;
char *string;
printf("Enter any string:

[Code] .....

But there is some problem in it, like its printing extra characters after string. reading infinite characters in a string

View 11 Replies View Related

C++ :: Reading Characters From String

Apr 13, 2013

i have a string which is n characters long. i need to read say 20 characters at a time, wait for the user to type OK and then send another 20 characters. wait for the user to type OK and send 20 characters again until we get to the nth character.

View 3 Replies View Related

C++ :: Reading String With Space?

Sep 26, 2013

how can I read some strings that contains spaces and put them in a vector of strings, using the push_back() function?

I have a collection of functions, for example: [multiply_by_forty two, add_by_five]. All I want to do is to store the strings like: multiply_by, add_by in a vector of strings, and the arguments:forty two, five etc in another vector of strings, but with spaces. The function convert() converts written numbers to numbers (for ex the output of covert("forty two")is 42;)

Here is the code:

public:void split() {
vector < string > s1;
vector < string > s2;

[Code].....

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/C++ :: Reading In A String And Turning It To Int

Dec 10, 2014

I have a problem with transforming a string, for example

"13 + 19"

and store this in a list as seperate integers,

list = {13, 19};

and another list with the +, -, /:

list2 = {+};

this is my function:

int evaluate(char* formula, int* result) {
struct List *listofintegers = list_create(); //creates a list, this is the structure:
/*
struct ListNode {
int value;
struct ListNode* next;

[Code] ....

This is how i execute my function in my main.c:

int value;
evaluate("19 + 16", value);

This is what i get in my prompt:

I will also have to seperate the +, - and / in another list,

I don't even know how i can get my string when char* formula is given as an argument...

View 2 Replies View Related

C :: Reading String And Checking If Integer

Nov 23, 2013

wrote this program to check if a string is an integer. It checks for + or - sign at the front of it, but it spat out some errors.I think I broke it.Here is the code:

Code:

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int getInteger(char*);
int main(void) {
char str[99];
int x;
}

[code].....

View 2 Replies View Related

C++ :: Reading Int Choice In With A String Buffer Instead Of Cin?

Sep 25, 2013

How do you read an int choice in with a string buffer instead of cin?

View 2 Replies View Related

C++ :: Error Reading Characters Of String

Feb 2, 2013

#include<fstream>
#include<Windows.h>
#include<cstdlib>
#include<iostream>
#include<string>
#include<iomanip>
#include<sstream>

using namespace std;

void add_matrix(int row1,int row2,int col1,int col2,double m1[30][30],double m2[30][30],double m3[30][30]);

[Code] .....

This program is suppose to read a matrix file , and the first getline is suppose to get the file header but it appears that 'line' doesn't take in any value other than empty thus causing all the problem , I tried to put cin.getline() in front of it to take away the /n created by the cin before it , but it doesn't work . When I debug the program when the arrow points to the string line , this error appears

line<Error reading characters of string.>std::basic_string<char,std::char_traits<char>,std::allocator<char> >

I tried to initialize string line=NULL too , doesn't work either.

View 1 Replies View Related

C++ :: Reading A File And Storing It In A String

Feb 7, 2013

I know how to do this in c++ with fstream and std::string and getline and so on and so forth. Im writing my code solely in c however. I can't get g++ installed so figured it was a good excuse to learn c instead of using the equivalent c++ abstracts.

My problem is, I'm making a game in c that I have made in c++ but have ran into an issue with my map. I want to read in my map from a file which just looks like this:
Name of Town
* * * * * * *
* * * * * * *
* * * * * * *
etc...

so i tried using fscanf to first read in the name of the town (stored in a char*) then read in the characters (in this case '*')(not including white spaces becuase i can just print those) into another char*. what is the better way to do this?

View 16 Replies View Related

C++ :: Error Reading Character Of String?

Jan 12, 2013

I am trying to record some information in a file and allow user to delete a record. I am facing this message in Autos section of MVS (Error reading character of string). Here is the code:

int removeRecord(string name, int &row)//remove a record
{
const string data="database.txt";
fstream records;
records.open(data.c_str());
const string cpData="temp.txt";

[code].....

If I delete the any row (except last roe) it works but then add a copy of last record (sometimes fully sometimes partially) at the end of the file!! if i delete the last record it does not do anything.

View 1 Replies View Related

C++ :: Reading String 2 Characters At A Time?

May 10, 2013

may i know how do i read a string 2 characters at a time?

lets say i have a for loop like this

for(int i=0;i<stringLen;i++)
{
for(int j=0;j<???;j++)
{
//insert code
}
}

what i want to do is i want to read a string 2 characters at a time and store them into a vector.

View 3 Replies View Related

C++ :: Writing / Reading String Objects To / From Files

Jul 7, 2013

I am using the code below to write a single instance of object "Employee" to a file in Binary mode. The write part seems to work fine, however when I try to read the single employee object from the file into memory I get a double free or corruption error.

I think this has to do with the fact that I am using a string data member in the Employee class but I don't understand what is going wrong. I have read that strings can vary in length and use dynamic memory allocation but if I write a single employee object to a file with data member 'name' equal to "John", it should be the exact same size when I read it back in right?

The code below works with no issues when I omit the string data member. Why is that? Where is the memory for the string object being "double released" when I read the employee object back into memory from the file?

I am using Linux Mint 15, Eclipse June and GCC 4.7.3 with the -std=c++11 option.

#include <iostream>
#include <fstream>
#include <string>

[Code].....

View 3 Replies View Related

C/C++ :: Reading A File And Replacing Characters With Corresponding String

Mar 27, 2014

So I am supposed to create a program that reads a file and replaces "<" with "<" , ">" with ">" , "&" with "&" , and " " " with """.........

The program takes a file "test.txt" and reads it, replaces the characters above with the corresponding strings, and then writes the output to "scrubbed.txt".

A sample input would be: <something>

and the output would be: <something>

for some reason I am getting garbage In my new file. What am I doing wrong with the code?

#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main(int argc, char **argv){
char *inFileName = NULL;
char *outFileName = NULL;
FILE *instream = fopen("test.txt", "r");
FILE *outstream = fopen("scrubbed.txt", "w");

[Code] ....

View 5 Replies View Related







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