Print a '-' after every 3 numbers from ID
Hi, I am currently working on a student records program and I want it to print a '-' after every 3 digits from the Id number that I read from the file(ID number from file doesn't have the dashes). How would I do that to print it with dashes? Thank you
Here are my reading functions.
Code:
void Read_Student(Student & Temp , ifstream & fin){
fin >> Temp.FirstName;
fin >> Temp.LastName;
fin >> Temp.Id;
fin >> Temp.Quiz;
for (int i = 0 ; i < 6 ; i++)
fin >> Temp.Test[i];
fin.ignore(10,'\n');
}
int Read_List(Student & Temp,Student List[], int Max_Size)
{
ifstream fin;
int i = 0;
if (Open_File(fin))
{
getline(fin, List[i].Course_Name);
getline(fin, List[i].Course_Id);
getline(fin, List[i].Course_Location);
Read_Student(List[i],fin);
while(!fin.eof())
{
i++ ;
if (i == Max_Size){
cout << "\nArray is full.\n" ;
break;
}
Read_Student(List[i],fin);
}
}
else
{
cout <<"\nBad file. Did not open. Program terminated.\n";
exit(0);
}
return (i);
}
SAMPLE INPUT:
Code:
Intro To Computer Science C++
SAL 343
JHG 344
John Adams
111223333 100 87 93 90 90 89 91
Willy Smith
222114444 90 73 67 -77 70 80 90
Phil Jordan
777886666 100 80 70 -50 60 90 100
One more thing, I also am having problems with reading last name. If I edited the inout file and had for example John Adams Jr. it would cause huge print problems. When I remove it it prints out perfectly. Any advice on that too please
Re: Print a '-' after every 3 numbers from ID
Quote:
If I edited the inout file and had for example John Adams Jr. it would cause huge print problems. When I remove it it prints out perfectly. Any advice on that too please
The extraction stream operator >> extracts the next element from the stream according to the type of the required extraction. The next element is an element of the required type delimited by white space (basically space, tab or newline). This for John Adams Jr, John as read as first name, Adams is read as LastName and Jr is read as Id. If the extraction operator tries to read an element that is inconsistent with the required type (eg tries to read qw34 as an integer) then the stream will enter 'fail state'.
Quote:
I want it to print a '-' after every 3 digits from the Id number
Your code doesn't state the type of the Id number - int, char*, string??
Re: Print a '-' after every 3 numbers from ID
Yes exactly, it does read it as Id. I tried get line but that doesn't work either since it picks up the whole line but how can I read Last Name as a whole with Jr. instead of removing it from the input file.
Re: Print a '-' after every 3 numbers from ID
Quote:
Originally Posted by
rephlex
Yes exactly, it does read it as Id. I tried get line but that doesn't work either since it picks up the whole line but how can I read Last Name as a whole with Jr. instead of removing it from the input file.
You will need to use a combination of extraction and getline to read the whole line. Something like
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string fn, ln;
cin >> fn;
getline(cin, ln);
ln = ln.substr(ln.find_first_not_of(' '));
cout << fn << endl << ln;
return 0;
}
This reads first name using extraction and then getline to retrieve the rest of the line.
Code:
John Adams jr
John
Adams jr
Input of John Adams jr is obtained as firstname John and lastname Adams jr.
Re: Print a '-' after every 3 numbers from ID
Re: Print a '-' after every 3 numbers from ID
Re: Print a '-' after every 3 numbers from ID
Quote:
Originally Posted by
rephlex
Id is of type int
If it's always 9 digits, I'd just convert it to a string and output a character at a time, outputting a - every three times through your loop.
Re: Print a '-' after every 3 numbers from ID
Try this. This will output any integer (32 bit positive signed) formatted with the delim char every 3 digits starting from the least significant.
Code:
#include <iostream>
using namespace std;
int main()
{
const int id = 234567891;
const char delim = '-';
for (int div = id, pwr = 1000000000, d, out = 0; pwr; div %= pwr, pwr /= 1000)
if (((d = div / pwr) == 0 && out) || d) {
cout << d;
out = 1;
if (pwr != 1)
cout << delim;
}
return 0;
}
This will output
Re: Print a '-' after every 3 numbers from ID
Quote:
Originally Posted by
2kaud
Try this. This will output any integer (32 bit positive signed) formatted with the delim char every 3 digits starting from the least significant.
Perhaps it would be better if he did his own homework.
Re: Print a '-' after every 3 numbers from ID
Quote:
Originally Posted by
GCDEF
Perhaps it would be better if he did his own homework.
Oops! Yeah... Still it is the season of goodwill to all. :wave: