Checking if there is a space after the first integer

A programming task that I'm doing says as one of the steps that the user has to input two numbers with a space after the first number. Here is a link to the task http://www.spoj.com/problems/PRIME1/

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <limits>

using namespace std;

void checkingCases(int &);
void checkingRange1(int &);
void checkingRange2(int &, int &);

int main()
{
    int counter = 0;
    int numTestCases; // The number of test cases to conduct.
    int range1; // The beginning of the first prime number.
    int range2; // The end with the last prime number.

    cout << "How many test cases do you wish to conduct? ";
    cout << "Note, the maximum number of test \ncases is ten. \n\n\n";
    cin >> numTestCases;

    checkingCases(numTestCases); // Check if the test case is valid.
    cout << endl << endl;

    while(counter < numTestCases)
    {
        cout << "Enter two numbers. \n\n\n";
        cin >> range1;
        checkingRange1(range1); // Check if the first range number is valid.

        cin >> range2;
        // Check if the second range number is valid and greater than range1
        checkingRange2(range2, range1);

        counter ++ // increment to the next case.
    }

    return 0;
}

void checkingCases(int &testNum)
{
    while(!(cin) || (testNum > 10 || testNum <= 0))
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        cerr << "This is not a number, a positive number or there are "
             << "too many test cases or no cases at all. ";

        cin >> testNum;
    }
}

void checkingRange1(int &num1)
{
    while(!(cin) || (num1 < 1 || num1 > 1000000000))
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << endl;

        cerr << "This is not a number, a number greater than or equal "
             << "to 1 and less than or \nequal to 1000000000, a positive "
             << "number. "<< endl << endl;

        cin >> num1;
    }
}

void checkingRange2(int &num2, int &num1)
{
    while(!(cin) || (num2 < num1 || num2 - num1 > 100000))
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << endl;

        cerr << "This is not a number, a number greater than or equal "
             <<  "to the first range, the difference between both ranges "
             <<  "is greater than 100000, or it's not a positive number. "
             << endl << endl;

        cin >> num2;
    }
}
Last edited on
A good approach.
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
#include <iostream>
#include <string>
#include <sstream>
#include <ctype.h>
using namespace std;

bool valid(string);

int main()
{
	int min,max;
	string str;
	
	cout<<"Enter min and max separated by a space: ";
	getline(cin,str);	//get input as string
	
	if(valid(str))
	{
		//extract the two numbers.
		stringstream stream;
		stream<<str;
		
		stream>>min>>max;	//get numbers
	}
	else
	{
		cout<<"Wrong input"<<endl;
		return 1;
	}
	
	cout<<"min = "<<min<<" and max = "<<max<<endl;
	
	system("pause");
	return 0;
}
//idea is that there must be exactly one space in the string.
bool valid(string str)
{
	int count = 0;	//number of spaces in str
	for(int i = 0; i < str.length(); i++)
		if(isspace(str.at(i)))
			count++;
	
	return count == 1;
}
@deathslice, Looking at your link now, you don't need to check whether there is a space. There would always be a space in the input. That is not in any way part of the concept that is tested by that question.

simple cin>>min>>max will work.
Also, you dont need to check the bounds of the input.checkingRanges() will also always be true.
No need to verify anything. Those constraints simply give you a pointer as to what data type you should use. basic int or long int and so on.

However, from your code above, it seems you are writing this program for a human to run.
So the checking might come in handy.
Last edited on
Alright, if I understand it correctly, what you did was start by getting input from the user, storing that input into a string variable, passed it on to the bool function to check If there is a space between both numbers. If there is, you parse the string and pass it onto both integer variables. Is that correct?
exactly!!
Well, in any case, What I do have to check for is the inequalities that are presented in the task(like t<=10, 1<=n<=m<=100000000) which is easy enough.

t being the number of test cases
n being range 1
m being range 2
Last edited on
Alright, I've updated it
Topic archived. No new replies allowed.