Code is stuck on an infinite loop, explanation why? [on hold]
Code is stuck on an infinite loop, explanation why? [on hold]
I'm really new to coding and need help fixing my code. Somehow, I've managed to get it stuck in an infinite loop. For some reason, whenever I add "for(int....) the loop begins. Don't know how to get it out without deleting for(int...) which I need in my code to add the judge number by 1 each iteration (1, 2, 3....5) for an iteration total of 5 loops. If there's another way to substitute for(int...) and getting the same outcome (minus the loop, obviously) I'd really appreciate it.
This is my code:
#include <iostream>
#include <iomanip>
using namespace std;
void getJudgeData(double & score);
int main()
{
double scoreOne, scoreTwo, scoreThree,
scoreFour, scoreFive;
//This is used to call the getJudgeData function
getJudgeData(scoreOne);
getJudgeData(scoreTwo);
getJudgeData(scoreThree);
getJudgeData(scoreFour);
getJudgeData(scoreFive);
}
void getJudgeData(double & score)
{
for (int i = 1; i<=5; i++)
{
cout << "Judge #" << i << " - Please enter a score between 0.0 and 10.0 :";
cin >> score;
while(score < 0 or score > 10)
{
cout << "Score must be between 0.0 and 10.0 : ";
cin >> score;
}
}
}
This is what the output becomes:
"Judge #1 - Please enter a score between 0.0 and 10.0 :1
Judge #2 - Please enter a score between 0.0 and 10.0 :2
Judge #3 - Please enter a score between 0.0 and 10.0 :3
......................
Judge #1 - Please enter a score between 0.0 and 10.0 :3
Judge #2 - Please enter a score between 0.0 and 10.0 :2
Judge #3 - Please enter a score between 0.0 and 10.0 :2
Judge #4.................."
This question appears to be off-topic. The users who voted to close gave this specific reason:
What if there's invalid input given to the
cin? The stream state goes to fail() and you'll never be able to read any value again, unless you clear() the stream.– πάντα ῥεῖ
Jul 1 at 7:05
cin
fail()
clear()
From what I see, this should prompt the user for input exactly 25 times. Have you seen more than 25 times?
– alter igel
Jul 1 at 7:05
After inputting 25 values it did stop, why is that? I changed the value of "i" to 1 and it completed the five iterations needed. How could I make it so that the judge value count is added by 1.
– RUBYXCUBE
Jul 1 at 7:11
getJudgeData() prompts the user for input 5 times, using the for loop. You call getJudgeData() 5 times. 5 * 5 = 25.– alter igel
Jul 1 at 7:12
getJudgeData()
for
getJudgeData()
1 Answer
1
You have called for getJudgeData() five times in your main function, and in each of which has a for loop that executes 5 times, so your code is executing 25 times as alter igel mentioned :
int main()
{
// 5 function calls
getJudgeData(scoreOne);
getJudgeData(scoreTwo);
...
}
void getJudgeData(double & score)
{
// execute for five times
for (int i = 1; i<=5; i++)
}
You can get your desired result by removing the for loop or you can use an array, as following :
#include <iostream>
#include <iomanip>
using namespace std;
void getJudgeData(double& score); // the same as before but withouth the for loop
int main()
{
double score[number_of_judges] ;
for (int i = 0; i < number_of_judges; i ++) getJudgeData(score[i]) ;
return 0 ;
}
Where number_of_judges is the number of judges you wish to have.
Please do not add irrelevant tags.
– cybermonkey
Jul 1 at 7:03