Bool function used in procedure not working as expected
Dec 9, 2013 at 1:21am UTC
Hello, this is my first post and I'm very new to programming. I need to create a bool function that is used in a procedure for validation.
What I want to do is: When user writes "0" bool should be false, otherwise do while stops iterating.
Help appreciated.
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
typedef struct ty_venta
{
string sFech;
int iCant;
int iCod;
};
bool isValidDate(ty_venta rVenta) //Corregir para que funcione
{
bool isValid;
if (rVenta.sFech == "0" )
{
isValid = false ;
}
else
{
isValid = true ;
};
return isValid;
};
void ingreso(ty_venta & rVenta, bool isValid)
{
do
{
cout << "ingrese fecha del show: " << endl;
cin >> rVenta.sFech;
}
while (isValid==false );
}
void main()
{
ty_venta rVenta;
bool isValid = isValidDate(rVenta);
ingreso(rVenta, isValidDate);
}
Last edited on Dec 9, 2013 at 1:22am UTC
Dec 9, 2013 at 4:30am UTC
I don't think you can do
void main()
(well without other special stuff that isn't for new guys)
I think this is what you are asking for.
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
#include <iostream>
#include <string>
using namespace std;
bool isValidDate(string a)
{ bool variable;
if (a == "0" ){variable = false ;}
else {variable = true ;}
return variable;
};
int main()
{
string number_string;
bool isValid;
do
{
cout << "ingrese fecha del show: " << endl;
cin >> number_string;
isValid = isValidDate(number_string);
}
while (isValid==false );
}
Last edited on Dec 9, 2013 at 4:45am UTC
Dec 9, 2013 at 4:46am UTC
Hey there Martin V.
I have a few comments:
Firstly, main() must be of type int and no other type (per the C++ standard).
Secondly, what's the point of having the boolean parameter 'isValid' present at all in the ingreso function? It seems to serve no purpose. What it appears you want to be doing is to check if an inputted date is valid, in which case you should call the isValidDate function as a while loop condition.
1 2 3 4
do
{
//...
} while (!isValidDate());
Otherwise the parameter 'isValid' is always going to be either true or false (and will never change) depending on the result of the first call to isValidDate inside of main.
Dec 10, 2013 at 12:01am UTC
Thanks a lot Thumper, your response really helped. This is the code that finally worked as expected:
1 2 3 4 5 6 7 8 9 10 11
void ingreso(ty_venta & rVenta)
{
do
{
cout << "ingrese fecha del show: " << endl;
cin >> rVenta.sFech;
}while (!isValidDate(rVenta));
}
Topic archived. No new replies allowed.