What Is The Error

I Want To Take Some Integers To An Array[1000]. To Do This Im Using A Loop.But The Program Says It Must End Taking The Values When The User Enters A Character. I Don't Know How To Do IT. It Keeps Looping When I Enter /a /character.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
main()
{
	int num[1000]={0},number=0;
	int temp=0,x=0;
	for(int j=0;x!=99;j++)
	{
        if(number<1000)
        {
              cout<<"Put: ";
              cin>>number;
              num[j]=number;
              number=0;
              temp=j;
        }  
        else
        {
              x=99;
              break;    
        }
    }
	numbers(num,temp);


The Other Thing Is I'm Passing These Numbers To A Function Which Will Remove All The Duplicate Numbers And Display Only Non Duplicated Numbers. Is This Right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void numbers(int num[],int j)
{
	int *nPtr;
	int fake[10]={0};
	nPtr=num;
   
    for(int i=0;i<10;i++)
    {
         for(int k=0;k<=j;k++)
         {
               if(k!=0)
               {
                       if(*(nPtr+i)!=num[k])
                       {
                       // fake[k+1];
                           fake[i]=*(nPtr+i); 
                                   
                       }
                             
                       else if(*(nPtr+i)==num[k])
                      {
                          cout<<num[k]<<"Duplicate"<<endl;
                     
                      }
               
                      if(i==0)
                      {
                          fake[i]=*(nPtr+i);        
                      }
               }
         }
         for(int x=0;x<j;x++)
         {
               //print 
               cout<<fake[x]<<endl;   
         }
    }
}



I Need To Know How To End The Loop When U Enter A Char??
Last edited on
You cannot enter a char when number is an int. An approach would be using a std::string and check that content. Like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <sstream> // for stringstream
#include <string>

int main()
{
	int num[1000]={0},number=0;
	int temp=0,x=0;
	string s; // Note
	for(int j=0;x!=99;j++)
	{
        if(number<1000)
        {
              cout<<"Put: ";
              cin>>s; // Note: now enter a string
              if(s == "a") // Check if it's an 'a'
                    break; // then break
              else
              {
                    stringstream ss(s); // Use the stringstream
                    ss>>number; // to convert s to int
                    num[j]=number;
                    number=0;
                    temp=j;
              }
        }  
        else
        {
              x=99;
              break;    
        }
    }
Thank you For Your Reply. Do You Know How To Modify This Code To Stop Looping For Any Alphabetical Character. I Didn't Know About SStream. I Will Start Researching About That. Thanks Again.
closed account (10oTURfi)
Do You Know How To Modify This Code To Stop Looping For Any Alphabetical Character

Didnt read your code but.
1
2
3
4
5
#include <cctype>

//...

if(isalpha(some_variable))break;
Ill try it. thank you
coder777 thank you for your answer.
Last edited on
Topic archived. No new replies allowed.