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:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
usingnamespace std;
void SelectionSort(int grades[], int count);
void BubbleSort (int names[], int count);
void Print(Str names[], int grades[], int count)
typedefchar 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!
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++;
}
}
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.
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>
usingnamespace 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
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 usingnamespace 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
Not an ignorant question. If you have declared usingnamespace 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.