command line

Hi, anyone know how to use to program this?

Write a C++ program that reads from the command line the names of 4 players and their scores for an on-line game. Your program should do the following :

1)Read in the values from the command line and display the name and the score of each player.

2) Find the player with the highest score.

3) Display the name and score of the player with the highest score.

for example, suppose the following values are entered in the command line :

Gary 48 Mary 96 Jack 90 Jil 28

The program should display

Name Score

Gary 48
Mary 96
Jack 90
Jil 28

Player with the highest score
Mary --> 96
"read from the command line" typically implies that your program is not yet running. For example,

C:\Michael\Prog\Foo> a.exe Gary 48 Mary 96 Jack 90 Jil 28

Name Score

Gary 48
Mary 96
Jack 90
Jil  28

Player with the highest score
Mary --> 96
C:\Michael\Prog\Foo> _

If you expect your program to be running when you get data, it is "read from standard input". For example:

C:\Michael\Prog\Foo> a.exe
Please enter 4 players and scores
> Gary 48 Mary 96 Jack 90 Jil 28

Name Score

Gary 48
Mary 96
Jack 90
Jil  28

Player with the highest score
Mary --> 96
C:\Michael\Prog\Foo> _

This makes a difference because input comes in two totally different ways. I suspect that you want the second method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
  {
  string   player_names[ 4 ];
  unsigned player_scores[ 4 ];

  cout << "Please enter 4 players and scores\n> ";
  for (int n = 0; n < 4; n++)
    {
    cin >> player_names[ n ] >> player_scores[ n ];
    }

  ... (do stuff with data)
  }

This is fairly simple stuff, so if you fell asleep in class or you feel your professor didn't explain it adequately, make sure to read through the tutorial:
http://www.cplusplus.com/doc/tutorial/basic_io/#cin

Good luck!
Sorry, not using this method. Have to be command line, argc, argv
Yup, I have read that before but still dun understand. Sorry

Sorry, not using this method. Have to be command line, argc, argv


you compile your program say named "myprog" then run it at the command line thus:

myprog abc def ghi

when you define your int main as follows:

 
int main(int argc, char* argv[])


argc holds the number of argments to your program including the program name, so in the above example argc would be 4, and the argv[] array is an array of char* of which argv[0] is your program name so:

argv[0] == "myprog"
argv[1] == "abc"
argv[2] == "def"
argv[3] == "ghi"

Does that help you now?
Topic archived. No new replies allowed.