filling spaces in a string.

This is a discussion on filling spaces in a string. within the C++ Programming forums, part of the General Programming Boards category; I'm having an issue with a loop to fill in spaces of a string so it can display a table ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    6

    filling spaces in a string.

    I'm having an issue with a loop to fill in spaces of a string so it can display a table properly. I dont know why its not working but it should; Ive tried everything with this thing and no go. Basicall I need the string studentName to loop, and the names to all be equal when I do a setw. Any help is appreciated.

    for (int x = 1; x <= 6 - nameSpaces; x++)
    oStream << 'x';


    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    void CalculateAverage(int, int, int, int, int, int &);
    char CalculateGrade(int);
    
    int main()
    {
        int average = 0;
    	int classAverage = 0;
        int grade1, grade2, grade3, grade4, grade5;
    	int class1 = 0;
    	int class2 = 0;
    	int class3 = 0;
    	int class4 = 0;
    	int class5 = 0;
    	int students = 0;
    	string studentName;
    	int nameSpaces = studentName.length();
    
        ifstream iStream;
    	ofstream oStream;
    
    	cout << "Working..."<<endl;
    
    	iStream.open("c:iStream.txt");
    	oStream.open("c:averages.txt");
    
    	oStream << "Name       Test1  Test2  Test3  Test4  Test5  Avg     Grade" << endl;
    	oStream << "-----      -----  -----  -----  -----  -----  ---     -----" << endl;
    
        iStream>>studentName;
    
    	while (iStream)
    	{
    	    iStream>>grade1;
    		class1 += grade1;
    
    	    iStream>>grade2;
    		class2 += grade2;
    
    		iStream>>grade3;
    		class3 += grade3;
    
    		iStream>>grade4;
    		class4 += grade4;
    
    		iStream>>grade5;
    		class5 += grade5;
    
            students++;
    
    		CalculateAverage(grade1, grade2, grade3, grade4, grade5, average);
    
    		classAverage += average;
    
    
    
    		oStream<<studentName;
    
    		for (int x = 1; x <= 6 - nameSpaces; x++)
    		oStream << 'x';  //<-----Problem here
    	
    		oStream <<setw(8)<<grade1<<setw(8)<<grade2<<setw(8)<<grade3<<setw(8)<<grade4<<setw(8)<<grade5<<setw(8);
    		oStream	<<average <<setw(8)<<CalculateGrade(average)<<endl;
    
            iStream.ignore(100, '\n');
    
    		iStream>>studentName;
    
    	}
    
        class1 = class1 / students;
    	class2 = class2 / students;
    
    	oStream<<"---------------------------"<<endl;
    	oStream<<setw(2)<<"Class"<<setw(7)<<class1<<setw(8)<<class2<<setw(8)
                <<class3<<setw(8)<<class4<<setw(8)<<class5<<setw(8)<<classAverage
    			<<setw(8)<<CalculateGrade(classAverage)<<endl;
    
    
    	iStream.close();
    	class3 = class3 / students;
    	class4 = class4 / students;
    	class5 = class5 / students;
    	classAverage = classAverage / students;
    
    
    	oStream.close();
    	cout<<"File ready."<<endl;
        return 0;
    }
    
    void CalculateAverage(int grade1, int grade2, int grade3, int grade4,
                            int grade5, int &average)
    {
        average = (grade1 + grade2 + grade3 + grade4 + grade5) / 5;
    	return;
    }
    
    char CalculateGrade(int score)
    {
        if (score >= 90)
    	    return 'A';
    
    	else if (score >= 80)
    	    return 'B';
    
    	else if (score >= 70)
    	    return 'C';
    
    	else if (score >= 60)
    	    return 'D';
    
    	else
    	    return 'F';
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    why x's?

    it seems to me like you just want to do

    cout << name << setw(strlen(studentName) - columnWidth) << ....... ;

    .....any way....namespaces has the value of zero.... i haven't looked real hard at your code, but just from skimming it i can tell you that that is your biggest problem...

    by the way... nameSpaces isn't a very good variable name because "namespace" is a c++ keyword
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    edit: double post
    Last edited by misplaced; 11-17-2004 at 06:38 PM. Reason: double post
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Code:
    int nameSpaces = studentName.length();
    At the time you set nameSpaces, studentName is empty, so nameSpaces is always 0. Your for loop will then output 6 'x's every time. I'm not sure exactly what you are trying to do, but maybe you should add the nameSpaces code just after you get the name before and in the loop.
    Code:
    iStream>>studentName;
    nameSpaces = studentName.length();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  3. removing spaces from a string
    By bradleym83 in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2005, 01:59 PM
  4. Replies: 3
    Last Post: 04-06-2005, 11:04 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21