Cin.get() Problem

I made this program that tells the user if the number that was inputted is prime or composite. At the end of a program I would normally create a char and then fill it but I wanted to try to do something more efficient. I wanted to use cin.get() but when I try to use it the program crashes. Here's the code:

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
41
42
43
44
45
46
47
#include <iostream>

int main()
{
    using namespace std;
    
    int input, test, ans = 0, i = 2;
    bool loop;
    
    cout << "Enter a number to be checked as a prime or composite number\n";
    cin >> input;
    
    while (i != input)
    {
          test = (input%i);
                  
          if (input == 1)
          {
          ans = 1;
          break;
          }
                           
          if (test == 0)
          {
                   ans = 0;
                   break;                   
          }
          else 
          {
                   ans = 1; 
          }          
          i++;
    }
    
    if (ans == 0)
    {
            cout << endl << input << " is a Composite Number\n";
    }
    else
    {
            cout << endl << input << " is a Prime Number\n";
    }          
    
    cout << "\nPress ENTER to quit\n";
    cin.get();
    return 0;
}     


The code itself works but I posted the entire thing just in case. Thanks in advance.
You need two of those cin.get() s...

-Albatross
Albatross++;

@MottMan
Are you really sure that your algorithm for primality testing is correct?... It says that 2 is a composite number...
Albatross++;

Is my name really an l-value? *checks*
Yep. It is. Uh-oh.
for(; Albatross > 0; Albatross--);

Aside from that, 2 is the initial value of i. If you make an exception to your algorithm, making it output that 2 is a prime number if 2 is entered, it would be alright.

Also, since it's more or less fixed, we could optimize it a bit. You only need to go through sqrt(input) (the plus one is for safety) iterations to determine if the number is prime or not. For larger prime numbers, this is one helluva time saver. You could change the program around a bit to deal with that...

-0
Last edited on
@Albatross

Thanks, it does work, but why do you need two? Also, I'm fairly new to C++ so this is about as optimal is I can get it but thanks for the ideas.

@m4ster r0shi

I didn't realize that it said two is a composite. I found quickly that one created an error but not two. I'll change that right now...

Why two cin.get() s? Not 100% sure. I'd have to dig around in the source files for that.

However, it seems that (this may be wrong) cin.get() tries to get a character from the stream, and indeed there is a newline to fetch that hasn't been gotten from the stream yet. When the next cin.get() comes, there's nothing to fetch, so it waits for the user to give it something to fetch.

-Albatross
@Albatross

Thanks, at least it makes a bit more sense now.



I changed my code to handle two now. Probably not in the most efficient way but here's the snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    int input, test, ans = 0, i = 1;
    bool loop;
    
    cout << "Enter a number to be checked as a prime or composite number\n";
    cin >> input;
  
    while (i != input)
    {
          if (input == 1 || input == 2)
          {
                   ans = 1;
                   break;
          }          
          
          ++i;
          test = (input%i);


If you find any other bugs please let me know.
Last edited on
However, it seems that (this may be wrong) cin.get() tries to get a character from the stream, and indeed there is a newline to fetch that hasn't been gotten from the stream yet. When the next cin.get() comes, there's nothing to fetch, so it waits for the user to give it something to fetch.


Yes. cin >> will leave '\n's in the stream.
I found a bunch of bugs and fixed them. Here is the new code:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>

int main()
{
    using namespace std;
    
    int input, oneortwo = 0, test, ans = 0, i = 2;
    
    cout << "Enter a number to be checked as a prime or composite number\n";
    cin >> input;

    if (input == 1 || input == 2)
    {
              ans = 1;
              oneortwo = 1;
                            
    }   
  
    while (i != input)
    {   
          if (oneortwo == 1)
          break;       
          
          test = (input%i);
          
          cout << "Test: " << test << "\t\ti: " << i << endl;
                          
          i++;
                          
          if (test == 0)
          {
                   ans = 0;
                   break;                   
          }
          else 
          {
                   ans = 1; 
          }          
                    
    }
    
    if (ans == 0)
    {
            cout << endl << input << " is a Composite Number\n";
    }
    else
    {
            cout << endl << input << " is a Prime Number\n";
    }          
    
    cout << "\nPress ENTER to quit\n";
    cin.get();
    cin.get();
    return 0;
}     


If there are anymore please let me know.

EDIT:
The line:

cout << "Test: " << test << "\t\ti: " << i << endl

is purely for debugging purposes. Feel free to take it out.
Last edited on
Topic archived. No new replies allowed.