im triying to convert an string to an integer and save those numbers into an array, i tried like this
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
int number[5];
string input;
//numbers
cout << "type sonme numbers"<<endl;
cin >> input;
for(int i = 0 ; i<= 4; i++){
number[i] = stoi(input.substr(i,i),0,10);
cout << number[i];
}
return 0;
}
when i run it this error comes out:
terminate called after throwing an instance of 'std::invalid_argument'
what(): stoi
Your first loop is asking for a substring beginning at index 0, with length 0, so you're passing an empty string to stoi. Even if you in fact provided valid inputs (a string of at least eight digits, so you could call .substr(4, 4) on it and get useful results), the first loop always tries to parse the empty string and dies. Don't do that.
It's unclear what the goal here is. If you meant to parse each digit independently, then what you wanted was:
number[i] = stoi(input.substr(i, 1), 0, 10);
which would parse out five sequential length one substrings.
Related
Out of all activity I've been tasked with, this was by far one of the most challenging one.
I am an IT student, ie a beginner in the C++ language, and so far I've only been taught how to make use of while loops and if conditions. By this, we have to use these two in the following activity:
Count how many of a certain digit is present on a given number.
Input two values in one line. The first one shall accept any integer from 0-9 and the other one shall take a random positive integer.
Using a while loop, count how many of the first integer (0-9) is present in the digits of the second inputted integer and print the result (see sample input and output for example).
In short, a program that reads the first value and checks how many of this first value is present in the second value.
Supposed Input: 2 1242182
Expected Output: 3
As you can see, it counts how many 2s are present in the second value (1242182). The result is 3 since there are three 2s present in it.
I find it impossible to create a program that can read 2 values in one line just using if conditions and a while loop, but this was given by us from an official programming website.
It should look like this.
#include <iostream>
using namespace std;
int main() {
int v1;
cin >> v1;
if (...) {
....
}
while (...) {
...
}
return 0;
}
Also, no using arrays.
#include <iostream>
using namespace std;
int main() {
int v1, v2;
cin >> v1 >> v2;
}
You need to get the two numbers, and then convert the second one to a string and iterate through that, checking it and incrementing.
#include <iostream>
#include <string>
using namespace std;
int main(){
int number, count, digit;
string num;
cin >> number >> num;
for(int i = 0; i < num.length(); i++){
digit = stoi(num[i]);
if(number == digit){
count++;
}
}
cout << "count: " << count << "\n";
}
#include <iostream>
#include <string>
using namespace std;
string cardsShuffle(string orig_seq){
string choice;
int random = 0;
string shuffled_seq;
int orig_len = orig_seq.length();
while(orig_len > shuffled_seq.length()){
random = rand() % orig_seq.length();
while(random % 2 != 0){
random = rand() % orig_seq.length();
}
choice = orig_seq.substr(random,2);
orig_seq.erase(random,random+2);
shuffled_seq = shuffled_seq + choice;
}
return shuffled_seq;
}
int main()
{
string orig_seq;
cout << "Enter orig_seq: \n";
cin >> orig_seq;
cout << cardsShuffle(orig_seq);
return 0;
}
This works perfectly until you try it with 10 characters then nothing is ever returned and the program just exist normally after going through the function as it normally does, except I can't figure out why it just decides it's done
I don't get a normal exit, I get "Floating point exception(core dumped)".
The erase function does not have the parameters you think it does - like substr, the second is the length, not the "one past the end" index.
(std::string has a peculiar interface, as it was created long before the standard collections were added.)
So you remove random+2 characters, and the longer the string, the greater the chance that you end up erasing too many characters, and that will lead to undefined behaviour.
Change that line to
orig_seq.erase(random, 2);
This exercise asks to take a input as a character array of number then add up the digits of the number.
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main() {
//Input a series of single digit numbers
char numbers[] = "a";
cout << "Input a series of single digit numbers." << endl;
cin >> numbers;
//convert the character array into a int array.
int sum = 0;
for (int i = 0; i < size; i++) {
sum += atoi(numbers[i]);
}
cout << "Sum of digits: " << sum;
return 0;
}
The atoi function, by my understanding, converts only whole character arrays (C strings) at a time, and I guess I cant step through the array, but it seems like this should work. My other option was to convert the Cstring to one large integer, then use the length of the string to step through and calculate the digits in each position but that's probably more inefficient that I could be making it.
What would you use to find single digits as ints for a character array?
char numbers[] = "a";
This creates an array of 2 char items. That's not sufficient for anything reasonable. Use a std::string instead.
cin >> numbers;
Better use std::getline from the <string> header.
sum += atoi(numbers[i]);
atoi takes a string as argument, not a single char. You want the sum of the digits, not the sum of the number values you get by applying atoi to all right substrings of the specification.
For a digit character ch, the corresponding digit value is ch - '0'.
I need help with this if statement. Im trying to read each character to see if it is a number. If it is not a digit then say it is not a number if it is continue reading on to the next character. for example if the user inputs 54gr 21 gr42 134 3f3. the only thing that would cout is 21 and 134.
#include <iostream> // libraries
#include <iomanip>
#include <string>
using namespace std;
int main()
{
char string[80];
// char num[80];
// char good[80];
cout << "enter a string "; // prompting user
cin.getline(string,80); // geting line
// int i = 0;
// int j = 0;
int count = 0;
{
while(string[count] != '\0') {
if(string[count] >= '0' && string[count] <= '9' )
cout << count << endl;
}
++ count;
}
}
I would not try to do this character by character. The problem is that you don't now that 5 is really part of a number until you've read to the end of the string of non-space characters to verify that all the contents are legitimately part of a number.
As such, I think you need/want to break the input up into "words", then check whether each complete word can be converted entirely to a number. You can read "words" with just some_stream >> some_string;
Once you have a "word" you check whether you can convert it entirely to a number. Assuming you want integers, you use strtol to (try to) convert it to a number. That will give you a pointer to the first character it couldn't convert as a number. If that's not the end of the string, then that "word" wasn't a number (even if it started with/contained one or more digits).
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How to convert a 2d char array to a 2d int array?
I'm trying to read input from stdin and stop reading when encountering EOF. I need to store these values as integers in a 2x2 array, array[i][j].
I'm reading in 81 Sudoku characters (integers) from a single line + 2 more characters (\n and EOF) for a total of 83.
Ex:
STDIN -- > 123414292142341......2\n <EOF>
How do I only store the numbers in each array[i][j]? and stop reading in when I encounter an <EOF> ?
So, after every 9 ints, I need to increment j to get the next row.
I'm looking to do this in C++
Thanks!
I have tried this so far
//Read in single line of 83 characters (81 sudoku integers + \n and
//Store each integer into array of corresponding row and column
#include iostream
using namespace std;
string input_line;
int main()
{
while(cin) {
getline(cin, input_line);
};
return 0;
}
How do I tokenize the string "input_line" into a character of integers? and then to atoi to convert to int, and then finally store them in the Sudoku array??
OK, thanks almost done. but I now keep getting an invalid conversion from 'char' to 'const char*' error!
#include <iostream>
#include <stdlib.h>
using namespace std;
string input_line;
int main()
{
char buffer[9][9];
int sudoku[9][9];
int v;
cout << "Enter input: " << endl;
cin >> (char*)buffer;
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++)
v = atoi(buffer[i][j]);
sudoku[i][j] = v;
If you want in C++ then you may choose to use C++ style cin. Following is pseudo code:
cout<<"Enter input:\n";
char sudoku[9][9];
cin>>&sudoku[0][0];
Here we are taking advantage of the fact that any digit will be less than < 256. So your table will be arranged inside the sudoku[][] automatically.