How to Create a Four-Digit Student Identification Number in C++?

  • Context: C/C++ 
  • Thread starter Thread starter spinner
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around creating a four-digit student identification number in C++, focusing on input handling and termination conditions. Participants explore different approaches to ensure the input is a valid four-digit integer and discuss how to terminate input when the user types 0.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant asks how to create a four-digit identification number that terminates input when 0 is entered.
  • Another suggests using an unsigned integer and a while loop to handle input and termination.
  • A participant shares a code snippet but expresses difficulty in ensuring the input is a four-digit integer.
  • One participant argues that a loop may not be necessary and suggests using an if-statement to check if the input is within the valid range for four-digit numbers.
  • Another participant clarifies the interpretation of the problem, suggesting that the requirement to terminate input upon entering 0 implies a loop is needed.
  • There is a discussion about the correct use of comparison operators in C++, emphasizing the difference between assignment and comparison.
  • A participant mentions that the problem is part of their midterm exam and expresses gratitude for the assistance received.

Areas of Agreement / Disagreement

Participants express differing interpretations of the problem requirements, particularly regarding whether a loop is necessary. Some believe a loop is needed to continuously accept IDs until 0 is entered, while others suggest a single input check may suffice. The discussion remains unresolved regarding the best approach.

Contextual Notes

Participants note the importance of using the correct comparison operators in their code, indicating potential misunderstandings in the original code snippets provided. There is also uncertainty about the exact requirements of the assignment.

Who May Find This Useful

Students learning C++ programming, particularly those working on input handling and control structures, may find this discussion relevant.

spinner
Messages
6
Reaction score
0
how can i make a student identification number to be a four digit integer and when you type 0 for the identification number it will terminates. thanks
 
Technology news on Phys.org
use an unsigned integer. when the id equals 0 break; or use a while loop.
 
while (idnum=4)
{
printf("Input ID number");
scanf("%d", &idnum);

if (idnum = 0)
break;
}
printf("ID number %d", idnum);

any correction from my while statement coz i can't get the exact 4 digit integer for the id number thanks in advance for your help
 
Last edited:
You shouldn't need a loop for this. Just read an integer and test it with an if-statement to see if it's in the proper range. What are the largest and smallest possible values for a four-digit ID number?
 
ok thanks jt ill try to work on it
 
oh i thought you wanted to keep taking in ID# until 0...other wise there's no point in using the word "termination". In the while loop do you know which equals sign to use? You want the comparative/relational operator. same with the if statement. My bad you just want it within a range. you what jtbell said is correct
 
my problem states that "student identification numbers are four-digit integers. input should terminate when i type 0 for the student identification number" sorry guys I am having a hard time wth the problem need help..
 
Well there are two scenarios from the posed question.

[0] That you need only to accept 1 ID and check that it is within range
[1] That you keep accepting IDs within range until 0 is inputted.(thus a loop)
the sentence "input should terminate when i type 0" to me would imply the second scenario for loop. Perhaps you should ask your professor(i'm guessing this is a homework problem) to clarify
 
Yeah, I was focusing on the four-digit integer requirement and your first guess at a solution which looked like you were trying to read four digits, one digit at a time.

If you've been studying loops in class just now, then neurocomp2003's scenario [2] is probably what you want. But if that doesn't clearly agree with the full statement of the assignment, then you should definitely ask your instructor.
 
  • #10
to jt and neo thanks guys for the help...im almost near with solution...actually its my midterm exam. its the only one that i left unsolved the rest are doing fine...best regards to both of you guys...
 
  • #11
BE VERY CAREFUL with your equals signs in if and while statements. The following if statement:

if (idnum = 0)

WILL NOT WORK. The single '=' means assignment; the result of this statement is that idnum is actually assigned the value 0, not compared with 0. The if statement will never run, because 0 means false.

What you want, instead, is the '==' operator, which means comparison:

if (idnum == 0)

- Warren
 
  • #12
spinner said:
actually its my midterm exam.

Are you allowed to use outside assistance on exams?
 
  • #13
Hahahahaha.....
 
  • #14
the admin can close the thread. problem already been solved thanks for the help guys...
 

Similar threads

Replies
1
Views
955
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 32 ·
2
Replies
32
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 17 ·
Replies
17
Views
2K