Skip to main content
GameDev.net gamedev.net
🔒 Locked

Creating String array??

Started by musafir2007 Mar 5, 2008 at 1:30 AM 1 replies 700+ views
Original Post
musafir2007
musafir2007
Ok so I am little stuck.. I have a input file.. Marks.txt -------------- Bob 59.5 Jack 53.2 Sam 89.5 Joe 66.4 Ram 73.4 -------------- I have to create a program that reads this file and prints the top grade and the name associated with it. I can't figure out how to create a array that can hold the names of people.. My code is still in the testing stage but any help with the String storing will be appreciated.

#include <iostream>
#include <fstream>
#include <string>

using namespace std; //introduces namespace std



int main ( void )
{
	double mark[50];
	char names[50];
	String.namess[50];
	
	ifstream inStream;
	
	inStream.open("Marks.txt");

	inStream >> names >> mark[0] >> mark[1] >> mark[2];
	
	cout << "String is " << names <<endl;
	cout << "Sum of " << mark[0] << " and " << mark[1] <<" is: "<< (mark[0]+mark[1]) <<endl;
	cout << "Sum of " << mark[1] << " and " << mark[2] <<" is: "<< (mark[1]+mark[2]) <<endl;
	
	return 0;
}

Thank you!
ToohrVyk
ToohrVyk
Arrays of initially unknown size are represented using std::vector, and strings are represented using std::string.

In modern C++, this would be done as such:

#include <string>#include <algorithm>#include <vector>struct person { std::string name; double mark; };std::istream &operator>>(std::istream &i, person &p){  return i >> p.name >> p.mark;}std::ostream &operator<<(std::ostream &o, const person &p){  return o << p.name << " " << p.mark << "\n";}bool operator<(const person &a, const person &b){  return a.mark < b.mark;}int main(){  std::vector<person> persons;  std::copy(std::istream_iterator<person>(std::cin),             std::istream_iterator<person>(),            std::back_inserter(persons));    if (!persons.empty())     std::cout <<       *std::max_element(persons.begin(), persons.end());}

Zahlman
Zahlman
Quote:
Original post by musafir2007
Ok so I am little stuck..
I have a input file..

Marks.txt
--------------
Bob 59.5
Jack 53.2
Sam 89.5
Joe 66.4
Ram 73.4
--------------
I have to create a program that reads this file and prints the top grade and the name associated with it.

I can't figure out how to create a array


First off, that does not require an array. Just remember the highest grade you saw so far, and the name that was associated with it.

As for your existing code:

1) You want to use some kind of loop, to handle each line separately.
2) 'string' is a class, and classes are data types. You use them basically just like primitives; i.e., you make a variable of that type. 'string name;' is a variable called 'name' which is a string, exactly the same way that 'double mark;' is a variable called 'mark' which is a double. Similarly, 'string names[50];' would be an array of 50 strings. But you still don't need arrays for this.
3) Also note that the name 'string' needs to be in lowercase.
4) You don't need to call .open() explicitly on a stream object; just pass the file name to the constructor, instead: 'ifstream inStream("Marks.txt");'.
5) Why would you calculate any sums if you're just looking for the biggest value?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.