Question:
Input validation is giving me a conversion error?
anonymous
2009-03-24 08:31:00 UTC
I must create a dynamic allocated array that holds a certain number of test scores entered by the user. It should not accept negative numbers. I've entered the code like I thought it should be, but I'm getting conversion errors. Can anyone please tell me what I'm doing wrong?



#include "stdafx.h"
#include
#include

using namespace std;


int main()
{
double *testScores, // To dynamically allocate an array
total = 0.0, // Accumulator
average; // To hold average test score

int numTests, // to hold the number of tests entered
count; // Counter variable

// Get the number of tests to be entered.
cout << "How many tests do you wish to enter? ";
cin >> numTests;

// Dynamically allocate an array large enough to hold
// that many tests scores.
testScores = new double[numTests];

// Get the test scores.
cout << "Enter the test scores: " << endl;
for (count = 0; count < numTests; count++)
{
cout << "Test " << (count + 1) << ": ";
cin >> testScores[count];
}
if (testScores < 1)
{
cout << "Test scores must be greater than zero." << endl;
}
Three answers:
?
2009-03-24 09:01:36 UTC
Your problem is



if (testScores < 1)



Here, what you're saying is "If the pointer 'testScores' is less than 1" which makes no sense.



What you should do is move the check inside the loop, and have it something like:



if (testScores[count] < 1)

{

//Do something

}



Ideally, I would do something like



double tmp; //temp var

bool valid = false;



. . .



for(count = 0; count < numTests; count++)

{

do{

cout << "Test " << (count + 1) << ": ";

cin >> tmp;

if(tmp >= 1)

{

testScores[count] = tmp;

valid = true;

} else {

cout << "Test scores must be greater than zero." << endl;

valid = false;

}

} while (!valid)

}



I know formatting makes it hard to read but in pseudo code it can be easier:



FOR However many scores the user wants to enter

:UNTIL the user enters a valid number

::Get input from the user

::IF input is valid

:::Save input and continue



Also note the above code wasn't tested at all, and just done off the top of my head and I'm pretty sure it has errors, but hopefully you can see what I was aiming for.



Good luck.
Berkut
2009-03-24 08:56:15 UTC
Here is the line with problem:

if (testScores < 1)



testScore is a pointer to the array and its value is address to the first of element of the array, so you cannot compare address to the memory with value in the memory.



Next whole semantics of this if statement is wrong and meaningless. Below is the modified source code implementing what you intended to do.

It has while statement which is called again and again until user inputs proper value (positive bigger than zero).



for (count = 0; count < numTests; count++)

{

while(true){

int temp;

cout << "Test " << (count + 1) << ": ";

cin >> temp;



if(temp < 1) {

cout << "Test scores must be greater than zero." << endl;

} else{

testScores[count] = temp;

break;

}

}



}
?
2016-12-01 06:18:15 UTC
it quite is so depressing and unfair. I stay in Canada and there is truthfully not something i will do. i don't be able to flow. i don't even think of i could desire to get a passport via then. the money and the area may be the genuine problems with out counting our 3 toddlers. i like to come back on R & S as equivalent, even however no person incredibly observed me different than some. nevertheless, this evening I felt unhappy and that i advised my husband that i could spend some time with my acquaintances on R & S. After that assembly, i'll actually be an intruder.(tears) Edit-i'm sorry for sounding so selfish, it quite is frequently not my nature. i'm quite satisfied for all of you and could be praying that each little thing is secure and beautiful for all and sundry of you who will attend. Love and God Bless.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...