entering arrays

This is a discussion on entering arrays within the C++ Programming forums, part of the General Programming Boards category; I am trying to get the firts part of my program running and this is what I have so far: ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    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?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    use getline to enter the line
    then pasre the line to extract the number
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    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...

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    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.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    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.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    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].

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21