Question

1. A Hogwarts student number starts with a letter and is followed by 4 digits., where...

1. A Hogwarts student number starts with a letter and is followed by 4 digits., where the first letter represents the student’s house (G, H, R and S), e.g. G7777, H1234, R4321 and S6666 are all valid. Write a program ValidateHSN that determines whether or not each student number is valid. Display a list of the student numbers and results. e.g. … G77777 invalid H1234 valid HH234 invalid.

e.g. … G77777 invalid

H1234 valid

HH234 invalid

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* I am writing C++ Program using files and I am create one text file

The text file contains (stdnumber.txt)

G7777
H1234
HH234
G77777
R4321
S6666
GG899
S98765
SSS45
H3850
GGGG1

*/

C++ PROGRAM

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
   ifstream in; // Declare ifstream object in
   string s; // declare string object s
   int i; // delcare integer variable i
  
   in.open("D:\\stdnumber.txt"); // read text file "stdnumber.txt" (read mode)
  
   if(!in) // check the file is invalid
   {
       cout<<"Invalid File name"; // display this message
       exit(0); // terminate entire program
   }
  
   while(in>>s) // create while loop for read strings in the file until end of file
   {
   i=1; // initialize i=0
   if(s.length()==5){ // check the string length=5 then,
     
   // check first letter should be G,H,R and S and also check the next character should be in between 0-9 numbers then,
       if((s[0]=='G'||s[0]=='H'||s[0]=='R'||s[0]=='S')&&(s[i]>='0'&&s[i]<='9'))
       {
       cout<<s<<" = valid"<<endl; // display valid string
       i++; // increment i value
  
   }
   else
   cout<<s<<" = invalid"<<endl; // else, display this messaage

   }
  
else
cout<<s<<" = invalid"<<endl; // display if string length!=5

  
  
} // end of while loop

  
  
   return 0;
}

OUTPUT-1

G7777 = valid
H1234 = valid
HH234 = invalid
G77777 = invalid
R4321 = valid
S6666 = valid
GG899 = invalid
S98765 = invalid
SSS45 = invalid
H3850 = valid
GGGG1 = invalid

OUTPUT-2

Invalid File name

Add a comment
Know the answer?
Add Answer to:
1. A Hogwarts student number starts with a letter and is followed by 4 digits., where...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 6. Consider a C program that reads two real numbers from the keyboard followed by a character where the character can b...

    6. Consider a C program that reads two real numbers from the keyboard followed by a character where the character can be one of the operators +,-, *, 1, or % providing the addition, subtraction, multiplication, division, or remainder respectively. Then the result is displayed on the screen For example, if the user types 2.0 3.0 % then your code will display: Note: In the above example the inputs are real numbers but the remainder only performs an integer operation....

  • Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns:...

    Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card...

  • Credit card numbers follow certain patterns. A credit card number must have between 13 and 16...

    Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. The number must start with the following: 4 for Visa cards 5 for MasterCard cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or is scanned correctly by a scanner. Almost all credit card numbers...

  • programming language: C++ *Include Line Documenatations* Overview For this assignment, write a program that will simulate...

    programming language: C++ *Include Line Documenatations* Overview For this assignment, write a program that will simulate a game of Roulette. Roulette is a casino game of chance where a player may choose to place bets on either a single number, the colors red or black, or whether a number is even or odd. (Note: bets may also be placed on a range of numbers, but we will not cover that situation in this program.) A winning number and color is...

  • (I would like this to be in five parts.) Write a program (Python module) that includes...

    (I would like this to be in five parts.) Write a program (Python module) that includes a function named studentrecords. This function must support execution of two loops. The first loop is to query the user whether they wish to enter additional records or not. It must accept responses of either yes or no as input, regardless of the letter-cases used for individual letters in the response. If the response is not recognized ( it isn’t yes or no) it...

  • Using C language, write a program for the following: In a student representative council election, there...

    Using C language, write a program for the following: In a student representative council election, there are ten (10) candidates. A voter is required to vote a candidate of his/her choice only once. The vote is recorded as a number from 1 to 10. The number of voters are unknown beforehand, so the votes are terminated by a value of zero (0). Votes not within and not inclusive of the numbers 1 to 10 are invalid (spoilt) votes. A file,...

  • Write a program that performs the following: 1. Presents the user a menu where they choose...

    Write a program that performs the following: 1. Presents the user a menu where they choose between:              a. Add a new student to the class                           i. Prompts for first name, last name                           ii. If assignments already exist, ask user for new student’s scores to assignments              b. Assign grades for a new assignment                           i. If students already exist, prompt user with student name, ask them for score                           ii. Students created after assignment will need to...

  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

  • I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the...

    I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the program should read your credit card (for example: 5278576018410787) and verify whether it is valid or not. In addition, the program must display the type (i.e. name of the company that produced the card). Each credit card must start with a specific digit (starting digit: 1st digit from left to ight), which also is used to detemine the card type according Table 1. Table...

  • Write a C++ program to compute the total volume of N cones, where N is a...

    Write a C++ program to compute the total volume of N cones, where N is a number entered by the user. Repeat the request for N until the user enters a positive number. For each cone, ask for the radius and height, compute the volume, and add that volume to the total. Each time you ask the user to enter either the height or the radius, if the user does not enter a positive number, display an error message and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT