Jump to content
  • Advertisement
Sign in to follow this  
musafir2007

Creating String array??

This topic is 3936 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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());
}


Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!