I am trying to read a decimal as a char and store it in an array by ignoring "." like if I have 0000.9876 will be stored as 9876. The program works fine...but it doesn't stop when i debug it.....I need help please ...........

#include<iostream>
#include <ctype.h>
using namespace std;

int main()
{
    char Ch;
    char Number[20];
    int l=0,k=0,j=0,NumberOfDigits;

    cin.get(Ch);

     while( k<20 &&(Ch = cin.get() ) != 0 )
    {


        if(Ch=='.')
        {

            while ( l<20 )
            {

                cin.get(Ch);
                if(isdigit(Ch))
                {
                    Number[l]=Ch-'0';
                    cout<<int(Number[l]);
                    l++;

                }
            }

        k++;    
        }


    }

    NumberOfDigits=l;

}

Recommended Answers

All 8 Replies

use code tag.
Do proper indentation to you code.

You're program is trying to get 20 sets of data after 20 different decimal points. I don't think that's what you want. You could move k into your inner while loop to keep track of the total number of digits.

Thanks for your help. I tried to put k in the inner loop .... again it is not working

>>like if I have 0000.9876 will be stored as 9876

If this is exact then, you are making it too complicated. Just have a variable that starts where the string has the '.' character then increment
it by one and then copy it.

string S = "000.123";
int stringIndx = 0;
//move index until the '.' character is found
while(S[stringIndx] != '.')
       stringIndx++;

//bypass the '.' character
stringIndx++;

//now copy from the string starting at stringIndx to its size

Thanks FIRSTPERSON....but how do I copy from the string starting at stringIndx to its size?

Thanks FIRSTPERSON....but how do I copy from the string starting at stringIndx to its size?

Use substr from the string library. If you want it as an int you can use atoi from there.

string number = S.substr(stringIndx); //Copies from stringIndx to end into number
int num = atoi(number.c_str()); //Get your number as an int

All Thanks for your Help.....In here I am not supposed to store '.' into my string....i improved the above code but still it is not working ....I have it here.....Why it is not stopping when i press enter???

#include<iostream>
#include <ctype.h>
using namespace std;

int main()
{
	char Ch;
	char Number[20];
	int K=0,NumberOfDigits;
	cin.get(Ch);
	while( (Ch = cin.get() ) != EOF)
	{
		if(Ch=='.')
		{
			while ( K<20  )
			{
				cin.get(Ch);
				if(isdigit(Ch))
				{
					Number[K]=Ch-'0';
					cout<<int(Number[K]);
					K++;
				}
			}
			
		}
		
	}


NumberOfDigits=K;
cout<<"\n"<<NumberOfDigits;

}

It requires you to hit enter twice, but put a if(Ch == '\n') break; before if(isdigit(Ch) and change EOF in the outer while to '\n' . I'm trying to think of another way to do it that's more elegant but that works.
Also, it's interesting (I'm not sure why) but it takes like 30 seconds on my machine (of less than a year) for the program to start accepting the chars I put in.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.