-
December 7th, 2013, 04:08 PM
#1
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
-
December 7th, 2013, 04:57 PM
#2
Re: Print a '-' after every 3 numbers from ID
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'.
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??
Last edited by 2kaud; December 7th, 2013 at 05:14 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 7th, 2013, 05:04 PM
#3
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.
-
December 7th, 2013, 05:40 PM
#4
Re: Print a '-' after every 3 numbers from ID
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 7th, 2013, 05:43 PM
#5
Re: Print a '-' after every 3 numbers from ID
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 7th, 2013, 05:56 PM
#6
Re: Print a '-' after every 3 numbers from ID
-
December 7th, 2013, 06:35 PM
#7
Re: Print a '-' after every 3 numbers from ID
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.
-
December 7th, 2013, 06:52 PM
#8
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
Last edited by 2kaud; December 7th, 2013 at 06:55 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 7th, 2013, 07:40 PM
#9
Re: Print a '-' after every 3 numbers from ID
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.
-
December 8th, 2013, 07:14 AM
#10
Re: Print a '-' after every 3 numbers from ID
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|