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
ADVERTISEMENT
Oct 26, 2012
Here`s the text:
You have to expect the following input : an arbitrary amount of lines, each line consists of 5 int32 numbers. The full input will be terminated by an EOF.
E.g.:
1 2 3 4 5
6 7 8 9 0
...
You`re then supposed to convert the numbers to integers and do some calculations. I would know how to parse a single line of 5 numbers via scanf(). That`s easy, and that`s exactly what they did in class.
But how do i go about splitting the lines? What about the EOF? Even if could hack something together, by using errno or something, it would be way beyond what they are doing atm. The input is received via user input, ie stdin.
View 1 Replies
View Related
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
Dec 2, 2013
Are there any examples how to parse a matrix:
Code:
const string ABC = "
A B C D
A 1 -1 2 14
B 0 -2 -4 8
C 6 2 2 3
" so if i have it as a string stream and then loop through each line like this:
Code:
istringstream in (ABC);
for (string line; getline(in, line); ){
vector<char> vec(line.begin(), line.end());
for (int i = 0; i< vec.size(); i++)
cout << vec[i] << "
";
}
I get my strings chopped into characters. but how to chop it into "meaningful" characters so that -1 is not - and 1. is there any quick way for that to happen ??
View 3 Replies
View Related
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
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
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
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
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
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
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
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
Jul 14, 2013
I am working on a text-based RPG game and I want to allow the player to save his progress. So I need to save several integers and a string. And my problem starts here "How can I save integers and load them?". I read the tutorial but I dont understand. I need to write a function to save game?
View 8 Replies
View Related
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
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
Mar 6, 2015
I am about to transfer a project I have written in Applescript and Objective C to Excel/VBA/dll. I have started with the Objective C functions that I want to place in a dll and call via VBA.
The Objective C is C with a thin dusting of special Obejctive C code to have it talk with Applescript and the rest of the project so in theory it should be easy to make dlls written in C from it.
But I have already problems with the tiniest of all functions. I am sure it can be done more effectively but right now I need to know WHY it doesn't work if I am ever going to be able to transfer the much larger functions from Objective C to C.
Here is my original Objective C code:
Code: -
(NSNumber *)game:(NSNumber *)games gamechange:(NSNumber *)gameskifte
{
int gamesab = [games intValue];
int gameskifteab = [gameskifte intValue];
[Code].....
View 5 Replies
View Related
Feb 10, 2013
The problem deals with writing a program to geta series of integers from a user and storing those values into an array. Then use a function called selection_sort, when given an array with n elements, the function must sort the array from smallest to largest values.
I have code, and im trying to get it to compile but im getting these implicit declaration errors and conflicting types. Here is my code and the compiler errors.
Code:
Compilation started at Sun Feb 10 20:14:48
gcc -Wall -o ex9-1 ex9-1.c
ex9-1.c: In function 'main':
ex9-1.c:16:5: warning: implicit declaration of function 'selection_sort' [-Wimplicit-function-declaration]
ex9-1.c:20:2: warning: implicit declaration of function 'prinf' [-Wimplicit-function-declaration]
ex9-1.c: At top level:
[Code] ...
Compilation exited abnormally with code 1 at Sun Feb 10 20:14:49
Code:
#include <stdio.h>
int main(void) {
int a[100], i, j, N;
printf("How many numbers will you be entering: ");
scanf("%d", &N);
[Code] .....
View 4 Replies
View Related
Feb 8, 2014
I understand most of program below. Essentially, we have strings that we want to convert from hex values to decimal equivalents. We check if first two characters of string are 0x or 0X, which signifies hex format. If our hex string consists of solely digits like 0x25, then the processing is simple. We take the digit assign it to answer variable, and for each additional position in the hex base-16 system, we multiply the digit by 16.
Now if the hex string is something like 0x2A, then for 'A', the hexalpha_to_int() function is called, since we are able to find 'A' in the hexalpha string, we take the value of 'A', which is ascii 65 divide it by 2 and add 10 to it: 65/2+10=42.5. This doesn't make sense. What is the purpose of this logic right here: 10 + (i / 2).
Code:
#include <stdio.h>
#include <stdlib.h>
int hexalpha_to_int(int c){
char hexalpha[] = "aAbBcCdDeEfF";
[Code] ....
View 1 Replies
View Related
Jul 28, 2014
My question is on c++ strings. At the moment my program is reading input in one line at a time after the user presses enter.
I want to read multiple values in on a single line. Example: "apple banana orange end" ... How would I do this?
MAIN Code:
#include "Header.h"
#include "Orange.h"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
[Code] ......
View 11 Replies
View Related
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
May 16, 2013
I need to check my understanding from some questions about strings. How can numeric values stored as Strings be converted to numbers?
a)vector of required numeric data type
b)atoi function in cstdlib library
c)cout statement with required numeric data type
I picked b), I am aware of atoi, atol, and atof as methods to convert, but are there other methods?
What is the purpose of strncat function?
Combines n characters from source string into target string
C++ string provides:
a)convenient way to declare and manage character arrays
b)functions to manipulate strings
c)all of the above
d)none of the above
I picked c)
View 2 Replies
View Related
Nov 11, 2014
My assignment is writing Madd Libs game. I still do not understand how to store inputted strings or values to arrays. I need explanation of collecting inputted data to arrays.
#include "stdafx.h"
#include<stdio.h>
//#include<string.h>
int main(void) {
char string[37] = {'