Help me with struct?

I've an assignment to do which is involving struct used. But,I'm puzzled to do it cuz,what if the user cin another wizard name than the provided? It'll sum up,I've to do it like,if the user cin wizard else than provided.It'll cout "Invalid wizard." But idk how to do it..
Anyone can help me with this? I'd really appreciate it :)

[THIS IS THE QUESTION]

From the film of Hansel & Gretel: Witch Hunters, Hansel & Gretel are bounty hunters who track and kill witches all over the world. Each and every single witch that was killed or burned, they have been rewarded a value of gold. The gold reward has been determined on the dead witch as shown in the table below:

Dead Witch Gold Reward (g)
Horned 2120
RedHaired 3503
Desert 3030
Muriel 5320
Etruscan 2512
Palsied 2305
Tumor 3300

Write a program in C++ that sum up the gold rewarded to the witch hunters and then determine the dead witch with the highest gold reward. The program should display the following output:

Enter the dead witch name and gold received in grams:
1. Horned 2120
2. RedHaired 3503
3. Desert 3030
4. Muriel 5320
5. Etruscan 2512
6. Palsied 2305
7. Tumor 3300

The total gold rewards are 22090 grams.
Muriel witch is the highest gold reward of 5320 grams.

Hints:
- A struct is required to accept the dead witch names with associated gold reward.
- A loop(s) to sum the total gold and find the dead witch with the highest gold reward.
- You might have a function (with parameters) that returns the highest gold reward.

But,I'm puzzled to do it cuz,what if the user cin another wizard name than the provided? It'll sum up,I've to do it like,if the user cin wizard else than provided.It'll cout "Invalid wizard." But idk how to do it..

You must validate the user input. Keep making them input a name until they make one that matches. A do...while loop is good for this kind of thing.
I've a copy of the coding like this (but even i put another name,it'll still sum up,what i wanna do is when the user cin another name than the provided,it'll not process anything,should i make a break statement? but well..how?)

#include <iostream>
#include <string>
using namespace std;

struct witch
{
string name;
int reward;
};

int totalgold(witch []);
witch highestgold(witch [],witch);

void main()
{
witch character[7],maxReward;

int total = 0;
cout << "Enter the dead witch name and gold in grams:\n";

for (int x = 0; x < 7; x++)
{
int counter = x + 1;
cout << counter << ".";
cin >> character[x].name;
cin >> character[x].reward;
}

total = totalgold(character);
maxReward = highestgold(character,maxReward);

cout << "The total gold rewards are " << total << " grams.\n";
cout << maxReward.name << " witch is the highest gold reward of " << maxReward.reward << " grams.\n";
}

int totalgold(witch character[])
{
int totalSum = 0;
for (int x = 0; x < 7; x++)
{
totalSum += character[x].reward;
}

return totalSum;
}

witch highestgold(witch character[],witch maxReward)
{
int x = 0, max = 0;
max = character[0].reward;
for ( x = 1; x < 7; x++)
{
if(max < character[x].reward)
{
max = character[x].reward;
maxReward = character[x];
}
}

return maxReward;
}
Last edited on
Well first of all you need to replace void main() with int main(). C++ standard requires that main() must return int. If this is compiling for you, I would strongly advise to change to an up-to-date compiler.

Second, please use code tags in future when posting code. You can find them on the Format: menu on the left when you're posting. ----->

Now onto your question...
I've a copy of the coding like this (but even i put another name,it'll still sum up,what i wanna do is when the user cin another name than the provided,it'll not process anything,should i make a break statement?


I actually have no idea what you're asking now and have become confused by the original question itself. lol Are the names in the table the only allowed names for the user to input? If so, are the allowed gold values in the table the only allowed gold values the user can input? Or can the user input any names and values?
Here's another way to go about it using a vector:

1st the include file:

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
#ifndef VECTOROFSTRUCTS_H_INCLUDED
#define VECTOROFSTRUCTS_H_INCLUDED

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class VectorOfStructs {
private:
  struct witch
  {
    string name;
    int reward;
  };
public:
  vector <witch> Witches;
  VectorOfStructs();
  void Initialize();
  void Process();
  ~VectorOfStructs();
};

#endif // VECTOROFSTRUCTS_H_INCLUDED 


Next the main routine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "VectorOfStructs.h"

using namespace std;

int main ()
{
  VectorOfStructs VofS;

  VofS.Initialize();
  VofS.Process();
}

VectorOfStructs::VectorOfStructs()
{
  // Create class object
}

VectorOfStructs::~VectorOfStructs()
{
  // Delete class object
}


Then the Initialization:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "VectorOfStructs.h"

using namespace std;

void VectorOfStructs::Initialize()
{
  // std=c++11 or gnu++11
  Witches.push_back ({"Horned",    2120});
  Witches.push_back ({"RedHaired", 3503});
  Witches.push_back ({"Desert",    3030});
  Witches.push_back ({"Muriel",    5320});
  Witches.push_back ({"Etruscan",  2512});
  Witches.push_back ({"Palsied",   2305});
  Witches.push_back ({"Tumor",     3300});
}


And the Process:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "VectorOfStructs.h"

using namespace std;

void VectorOfStructs::Process()
{
  int NumWitches  = Witches.size();
  int TotalReward = 0;

  // List all rewards
  cout << "Dead Witch Gold Reward (g)" << endl;
  for(int it = 0; it < NumWitches; it++)
  {
    cout << "Name: " << Witches[it].name << " Reward: " << Witches[it].reward << endl;
    TotalReward += Witches[it].reward;
  }
  cout << endl << "Total Reward: " << TotalReward << endl;
}


I used separate files for each to keep it neat.

Largins
Topic archived. No new replies allowed.