c++ how to use while properly

Viewed 73

I am learning c++ around two weeks and therefore have a lot of questions. It feels like i learn a new sport. My body in my thinking already moving much better than any other olympic players, but the actual movement is so poor.

what i want to know is if i can use "while" in cout together.

int main() {
struct {
    string engineType;
    string brand;
    int price;
    int range;
} candidate1, candidate2;

// information of candidate 1
candidate1.name = "IONIQ5";
candidate1.range = 450;
candidate1.price = 35000;

// information of candidate 2
candidate2.brand = "Tesla_Model_3";
candidate2.range = 650;
candidate2.price = 55000;

// show each price with while function
int i = 1;
while (i<3) {
    cout << "Price :" << candidate[i].range << endl ;
    i++;
}
return 0;

I want to have as a result of print

450
650

what do i have to do to get it ?

Thanks for the help !

4 Answers

Use an array and initialize it.

First you need to name your structure:

struct candidate {
    std::string engineType;
    std::string brand;
    int price;
    int range;
};

Then create and initialize the array:

std::array<candidate, 2> candidates = {
    { "Electric", "IONIQ5", 35000, 450 },
    { "Electric", "Tesla_Model_3", 55000, 650 }
};

And finally iterate over it:

for (auto const& candidate : candidates) {
    std::cout << "Price: " << candidate.price << '\n';
}

All this should be well-covered in a decent book.

You cannot use candidate1 like candidate[i] because it works as a variable name . You need to use maps or arrays for this type of tasks.

You can use an array with elements of type Candidate and then loop through the array and print the values as shown below:

//class representing a Candidate info
struct Candidate{
    string engineType;
    string brand;
    int price;
    int range;
};


int main() {

    //create an array witht element of type Candidate;
    Candidate arr[] = {{"IONIQ5", "Honda", 450, 3500}, {"Tesla_Model_3", "Tesla", 650, 55000}};
    
    //iterate through the array using range based for loop 
    for(const Candidate& candidate: arr)
    {
        std::cout<<candidate.range<<std::endl;
    }
}

Working demo

You are declaring 2 separate variables, called candidate1 and candidate2 as an array, so candidate[i] does not resolve to anything.

You need to initialize the array as follows:

main ()
{
  struct 
  {
    string engineType;
    string brand;
    int price;
    int range;
  } candidate[3];

    
// information of candidate 1
  candidate[1].brand = "IONIQ5";
  candidate[1].range = 450;
  candidate[1].price = 35000;

// information of candidate 2
  candidate[2].brand = "Tesla_Model_3";
  candidate[2].range = 650;
  candidate[2].price = 55000;

// show each price with while function
  int i = 1;
  while (i < 3)
    {
      cout << "Price :" << candidate[i].range << endl;
      i++;
    }
  return 0;
}

Please note that arrays start with 0, so an array of 3 will have values for postitions 0, 1, 2, hence the candidate[3], but I would recommend going with candidate[2] and setting values for positions 0 and 1.

Related