Print a '-' after every 3 numbers from ID
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Print a '-' after every 3 numbers from ID

  1. #1
    Join Date
    Oct 2013
    Posts
    5

    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

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    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 04:14 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Oct 2013
    Posts
    5

    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.

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Print a '-' after every 3 numbers from ID

    Quote Originally Posted by rephlex View Post
    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Print a '-' after every 3 numbers from ID

    What's the type of Id?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Oct 2013
    Posts
    5

    Re: Print a '-' after every 3 numbers from ID

    Id is of type int

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,055

    Re: Print a '-' after every 3 numbers from ID

    Quote Originally Posted by rephlex View Post
    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.

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    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
    Code:
    234-567-891
    Last edited by 2kaud; December 7th, 2013 at 05:55 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,055

    Re: Print a '-' after every 3 numbers from ID

    Quote Originally Posted by 2kaud View Post
    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.

  10. #10
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Print a '-' after every 3 numbers from ID

    Quote Originally Posted by GCDEF View Post
    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center