how to take numbers from int and put them on table 1 by one
hi.... im trying to figure an easy way, how to take numbers from int and put them on table 1 by one. for example 12345 would go to table as {5,4,3,2,1}. hopefully someone could help me on this :)
Hi.. i tried this with string... so i would read the given number into string.. then move then re arrange it bacwards... and then putting it into int. here's the code:
string numbers; string temp; int total; int arranged[20]; int multiple []={7,3,1,7,3,1,7,3,1,7,3,1,7,3,1,7,3,1,7,3};
cout<<"give a number: "; cin>>numbers;
for(int i=numbers.length(), int j=0; i>0; i--, j++) { std::istringstream help(numbers.substr(i-1,1)); help >> arranged[j]; total+= help*multiple[j]; cout<<arranged[j]; cout<<total; }
return 0; }
it gives error : C2676: binary '*' : 'std::istringstream' does not define this operator or a conversion to a type acceptable to the predefined operator
how to take numbers from int and put them on table 1 by one
how to take numbers from int and put them on table 1 by one
Carlos Figuera.
What have you tried so far
Thanks, Ayman Shoukry VC++ TeamRodolfo Montero
You couldn't make my code work Really Because I tested it before I posted it here. What compiler are you using Did you include these headers
zille
I think this is what you want:
spattewar
is there a simple way to move numbers from Char to an Integer table.... like 12345 would put 5 to int[0]spot, 4 would go to int[1] spot... and so on.
help would be appriciated, since im really stuck on this :(
tulip-pcsc
So you can learn by example. Not as elegant as Marius' answer ... I don't use the Standard C++ lib that deeply.
int main()
{
string s;
cout << "Enter a string of digits: " << endl;
getline( cin, s );
vector<int> int_table;
for( size_t i = 0; i < s.length(); i++ )
{
if( s[ i ] >= '0' && s[ i ] <= '9' )
{
int_table.push_back( s[ i ] - 48 ); // subtract ascii of '0' to 0-base values
}
else
{
cerr << "Ignoring invalid character: " << s[ i ] << endl;
}
}
for( size_t i = 0; i < int_table.size(); i++ )
{
cout << "int_table[" << i << "]=" << int_table[ i ] << endl;
}
}
Anthony von LA
but the problem with this code is that i cant multiple those re arranged numbers one by one.... for that i tried this code :
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string numbers;
string temp;
int total;
int arranged[20];
int multiple []={7,3,1,7,3,1,7,3,1,7,3,1,7,3,1,7,3,1,7,3};
cout<<"give a number: ";
cin>>numbers;
for(int i=numbers.length(), int j=0; i>0; i--, j++) {
std::istringstream help(numbers.substr(i-1,1));
help >> arranged[j];
total+= help*multiple[j];
cout<<arranged[j];
cout<<total;
}
return 0;
}
it gives error : C2676: binary '*' : 'std::istringstream' does not define this operator or a conversion to a type acceptable to the predefined operator