-
entering arrays
I am trying to get the firts part of my program running and this is what I have so far:
Code:
#include <iostream>
using namespace std;
const int Size = 20;
int main ()
{
char FirstNumber [Size];
char SecondNumber [Size];
int Idx;
int Idx2;
int Num1 [Size];
int Num2 [Size];
//input
cout<<"Enter the first number, less than 20 digits long, please : ";
int Count = 0;
while (Count < Size && cin>>FirstNumber [Count])
{
Count++;
}
for ( Idx = 0; Idx < Count ; Idx++)
{
cin>>FirstNumber [Idx];
int Num1 [Idx] = FirstNumber [Idx] - '0'; //changing char to int
}
cout<<"Enter your second number, less than 20 digits long, please";
int Count2 = 0;
while (Count2 < Size && cin>>FirstNumber [Count2])
{
Count2++;
}
for (Idx2 = 0; Idx2 < Count2 ; Idx2++)
{
cin>>SecondNumber [Idx2];
int Num2 [Idx2] = SecondNumber [Idx2] - '0';
}
return 0;
}
So I enter the first number but then nothing happens, it doesn't go on to ask for the second number. I am trying to count how many sinlge digits are entered and then trying to read in those digits into the array one by one. Also when I try to convert the characters to integers I get an error. Any one have nay ideas?
-
use getline to enter the line
then pasre the line to extract the number
-
no to be sarcastic, but what exactly are you hoping to achieve with this program? Looks like a bunch of over-complicated code for no apparent reason...
-
for an assignment I am suppose to ask the user for two numbers less then 20 digits long and put them in an array as characters and then change them to integers and then find the sum of the two numbers by adding each digit of the arrays together. I am trying to read in the numbers if the user enter less then 20 digits. If I enter 20 digits it works fine but I need to be able to read if it is less then 20.
-
The way you have it written, the two numbers must be exactly 20 characters long, no less. You need someway to exit the loop if a non-digit character is entered.
Also you seem to get each number from cin twice; you have two loops that get the number from cin.
-
what i would do to get the first number is the following:
Code:
char fstNum[28];
for (int i = 0; i < 20; i++) {
cin >> fstNum[i];
if (fstNum[i] == '') { break; }
}
...or something like that...
...and the same could be done for the second nuber...
...i made a char with [28] jst to be safe, cuz it could b dangerous if u tried to put something into fstNum[21], and u only had up to [20].
-
PS: I haven't tried that, so it's just a vague idea of what I might attempt to do if I was in your situation.