Alphabetical/Numerical Order

I'm working on the code for a class assignment, and I would like a nudge in the right direction with regard to reading strings from a text file. Here's the code I have, thus far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;
void SelectionSort(int grades[], int count);
void BubbleSort (int names[], int count);
void Print(Str names[], int grades[], int count)

typedef char Str[10];
int main(void)
{
	Str names[20];
	int grades[20], i=0;
	char fn[30];
	cout << "Enter a file name: ";
	cin >> fn;
	ifstream infile(fn);
	if (infile)
	{
		for ( ; infile >> names[i] >> grades[i]; i++)
			if (

			}

	}


}

void SelectionSort(int grades[], int count)
{
	for (int i=0; i < count-1; i++)
	{
		int largest=i;
		for (int j=largest+1; j < count; j++)
			if (grades[j] > grades[largest])
				largest=j;
		int tmp=grades[largest];
		grades[largest]=grades[i];
		grades[i]=tmp;
	}
}

void BubbleSort (int names[], int count)
{
	for (int i=0; i < count-1; i++)
	{
		for (int j=0; j < count-1; j++)
			if (names[j+1] > names[j])
			{
				int temp=names[j];
				names[j]=names[j+1];
				names[j+1]=temp;
			}
	}
}

void Print(Str names[], int grades[], int count)
{
	cout << endl;
	for (int i=0; i < count; i++)
		cout << left << setw(15) << names[i] << right << setw(3) << grades[i] << endl;
	cout << endl;
}


The assignment is simply to read the names and associated grades from a text file containing the following and putting them in order:

Brandon 95
Kim 85
Steve 75
John 60
Stacy 50

I'm very comfortable with the functions that are being used and their logic, but, oddly enough, the part with which I'm having trouble is what to do in order to read the text file, properly. I'm not looking for an answer to copy; I'd just like a little light shed on the direction to go. Any help is much appreciated!
Last edited on
Please, use code tags [code]Your code[/code] in order to make your code readable

What you need is to read it line by line and then get each element:
1
2
3
4
5
6
7
8
9
10
11
while(infile.good())
{
  std::string line;
  std::getline(infile, line); // retrieve the line
  if(not line.empty()) // ignore empty lines
  {
    std::stringstream ss(line); // stringstream is necessary to parse the line
    ss >> names[i] >> grades[i]; // get the data from the stringstream
    i++;
  }
}
For stringstream read http://www.cplusplus.com/reference/iostream/stringstream/ and #include <sstream>
Using 'string' (#include <string>) makes life easier but there're ways to do it without

Notice that it's not tested
Thank you, coder! Actually, our instructor has never used "::" in any of the presentations/examples we've been shown or given; the same goes for stringstream. Using "getline(infile, line)" is definitely a help, as we've been shown as much. What exactly do the double-colons indicate?

edit: Actually, I just read the definition for the double colons. I guess I should be asking what is used *instead* of those.
Last edited on
The :: is if you don't use that namespace or member of that namespace. Here are a few examples:
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std; //We just told the compiler that we want to include
                     //everything in the std namespace in the global namespace too
int main()
{
  cout << "Hey, world." << endl; //We can use cout and endl directly
  std::cout << "How are you?" << std::endl; //Or we can still go through std:: if we want
}
1
2
3
4
5
6
7
#include <iostream>
using std::cout; //We want to include std::cout in the global namespace, but nothing else

int main()
{
  cout << "Hi again" << std::endl; //cout can be used without std::
}                                  //but we have to go through the std:: namespace to get to endl 
Last edited on
I see what you're saying. This question is likely ignorant, but what purpose does it serve to consistently type out the double colons if you can avoid it by (in this case) using the standard namespace?
The point of namespaces is to avoid naming conflicts, such as two classes by the same name, functions by the same name, global variables by the same name, etc.. Namespaces solve the issue of these unforseen events that could happen from including two different libraries. If you start using namespace for all the namespaces you come across, you will very likely encounter this problem. If iot weren't for namespaces, you might actually have to modify headers and change the names of everything and everywhere it is used...etc, it can get really messy. Namespaces are important :D
closed account (D80DSL3A)
Not an ignorant question. If you have declared using namespace std; then there is no need for the std:: appearing in coder777's code.
FYI: The :: is the scope resolution operator.

I just tested coder777's solution. I had to change if(not line.empty()) to
if( !line.empty()) but then it worked.

I wrote this solution yesterday and was going to post it until I saw that coder777 had beaten me to it. It is tested and also works for the given situation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>
#include <sstream>// needed for coder777's solution
#include <string>
using namespace std;	

int main()
{
	string names[20];
	int grades[20], i=0;
	char fn[30];
	cout << "Enter a file name: ";
	cin >> fn;
	ifstream infile(fn);

	if(infile)
	{
		while(!infile.eof())
		{
			infile >> names[i] >> grades[i];
			if( ++i >= 20 )break;// arrays are FULL
		}
		infile.close();
		// output the array contents to test the file read
		for(int n=0; n<i; ++n)
			cout << names[n] << " " << grades[n] << endl;
	}
	else
		cout << "file " << fn << " was not opened." << endl;

	cout << endl;	
	return 0;
}

Both solutions fail if there are any spaces in the names, eg.

Brandon Lee 95
Kim 85
Steve 75
John 60
Stacy 50

But mine fails worse. coder777's fails on the 1st line but recovers and processes the remaining lines correctly.
Topic archived. No new replies allowed.