if-statement wont work

Hello Everyone I'm new to to this forum. I'd appreciate it if someone experienced helped me with fixing my program.
I Need to write a program that validates a number. The number can't have any non-numeric characters in it, and it should be 8 numbers long like 12345678. I've managed to write the code to check whether it's 8 characters long but i cant get it to check whether it contains non-numeric characters. If it does contain non-numeric characters, it should change the student number to 55555555 and increment a counter. There are a few other criteria like asking for a name and changing the number to 11111111 or 99999999 if the number is less than or more than 8 characters but i got that working. My only problem is getting the while loop and the contained if-statement to work to change the student number to 55555555. This is my 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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;
const int LENGTH = 8;
int main()
{	bool onlyNumbers = true;
	int errorCounter =0;
	int count = 0;
	string studentNumber, name, newStudentNumber;
	cout << "Enter your name: ";
	cin >> name;
	cout << "Enter your student number: ";
	cin >> 	studentNumber;
	newStudentNumber = studentNumber;
	// Testing if string contains non-digit numbers
	while ( count <= studentNumber.length()) // 
	{count = count +1;
		if (((studentNumber[count]) <= '0') || (((studentNumber[count])>= '9'))) --> I suspect my error is here
		{onlyNumbers = false;
			newStudentNumber = "55555555";
			}}
			if (onlyNumbers = false)
				{count = count +1;
			}
		if (studentNumber.length() < LENGTH)
		{errorCounter = errorCounter+1;
			newStudentNumber = "11111111";
		}
		if (studentNumber.length() > LENGTH)
		{errorCounter = errorCounter+1;
			newStudentNumber = "99999999";
		}
		cout << name << endl;
		cout << newStudentNumber << endl;
		cout << "Number of errors = " << errorCounter << endl;
	return 0;
}	

To me the error appears to be in using "<" and "<=" signs. for ex, instead of

1
2
while ( count <= studentNumber.length()) // initial count = 0;
	{count = count +1;


try

1
2
while ( count < studentNumber.length()) // initial count = 0;
	//{count = count +1; move this statement to the end of while loop. 


also instead of

 
if (((studentNumber[count]) <= '0') || (((studentNumber[count])>= '9')))

try
 
if (((studentNumber[count]) < '0') || (((studentNumber[count])> '9')))


also, i don't understand the need for the following statement:
1
2
3
if (onlyNumbers = false) // (should be ==)
  {count = count +1;
        }
Last edited on
Hi! I'm a beginner to proging in C++.
I've tried to play with your code.
It is now similar to that you've written above.

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
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;

const int LENGTH = 8;

int main()
{
    	bool onlyNumbers = true;

	int errorCounter =0;
	int nonNumCounter =0;
	int NumCount = 0;
	int Asc=0;

	string name, studentNumber, newStudentNumber;

	cout << "Enter your name: ";
	getline(cin,name); // cin  >> name;

	cout << "Enter your student number: ";
	cin  >> studentNumber;

	cout << endl;
	newStudentNumber = studentNumber;

	// Testing if string contains non-digit numbers

	while ( NumCount < studentNumber.length() )
	{
        	Asc = studentNumber[NumCount];
        	cout<<"position("<<NumCount<<") --> "<<' '<<"AsciiCode: "<<Asc<<endl;

        	// ascii codes of 0 and 9 --> http://www.cplusplus.com/doc/ascii/
		if ( (Asc<48) || (Asc>57) )
		{
		    onlyNumbers = false;
			newStudentNumber = "55555555";
			nonNumCounter++;
                }
                NumCount++;
    	}

    	// if (onlyNumbers == false)
   	//    nonNumCounter++;

    	if (studentNumber.length() < LENGTH)
    	{
        	newStudentNumber = "11111111";
        	errorCounter++;
    	}

    	if (studentNumber.length() > LENGTH)
    	{
      		newStudentNumber = "99999999";
        	errorCounter++;
    	}

    	cout << endl;
    	cout << "name :             " << name << endl;
    	cout << "newStudentNumber : " << newStudentNumber << endl <<endl;
    	cout << "Number of errors : " << (errorCounter + nonNumCounter)
	        << endl << endl;

	return 0;
}
Last edited on
What is the error that you are getting?
thank you abishekm your advice was extremely helpful
Topic archived. No new replies allowed.