void function

does complie but getting wrong output

Page 1 of 1

8 Replies - 1353 Views - Last Post: 18 June 2007 - 12:01 PM Rate Topic: -----

#1 Simply   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 07

void function

Posted 18 June 2007 - 04:53 AM

In the program below values are assigned to two variables by inputting them in the void function inputAndValidate. The two values are made available to the main function via the parameter list – note the use of & in the function header. Further note the condition of the do..while loop – the loop is repeated over and over until a correct code is input, thus the loop is executed again if the code is not equal to one of the three correct codes. We call this
validation of the data. (You could have set up the condition differently, for example
(!( (code == "ABC111") || (code == "DEF112") || (code == "XYZ113")))

The two value parameters in the function header of updateCorrectTotal contain the values of the specific module code and the number of hours that should be added to one of the totals. These values will not be changed inside the function. However, the three totals are all reference parameters (note the &) since their values may be changed in the function and then should be available in the main function. The body of the function involves the checking of the module code to determine which total should be updated and the addition of the hours to the correct total.

The main function contains a for loop where the two functions written above are called. When the loop has been executed NUMBER times, the totals are displayed.

It does compile but my do loop is not working properly and it does not ask me about the time – WHY?????

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

const int NUMBER = 2;

//Function inputAndValidate
void inputAndValidate(string & code, int & hours)
{
do
{ //Start of do loop
cout << "Enter a module code: ";
cin >> code;
} while ((code != "ABC111" ) || (code != "DEF112" )|| (code != "XYZ113" ) || (code != "abc111" ) || (code != "def112" )|| (code != "xyz113" )); //Condition of loop

cout << "Enter number of hours: ";
cin >> hours;
} //End of Void function


//Function inputAndValidate
void updateCorrectTotal(string code, int hours,int & totalABC, int & totalDEF, int & totalXYZ)

{ //Beginning of void function
if (code == "ABC111")
	 totalABC += hours;
else if (code == "DEF112")
	 totalDEF += hours;
else
totalXYZ += hours;
} //End of void function

int main( )
{
string moduleCode;
int numberHours, totABC, totDEF, totXYZ;

// Counters
totABC = 0;
totDEF = 0;
totXYZ = 0;

// loop
for (int i = 1; i <= NUMBER; i++)
{
//Void inputAndValidate
inputAndValidate(moduleCode, numberHours);

//Void updateCorrectTotal
updateCorrectTotal(moduleCode, numberHours, totABC, totDEF, totXYZ);
} //End of loop

// Display 
cout <<"ABC111" << endl;
cout << "DEF112" << endl;
cout << "XYZ113" << endl;

cout << totABC << endl;
cout << totDEF << endl;
cout << totXYZ << endl;
return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: void function

#2 Glasseater   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 15-June 07

Re: void function

Posted 18 June 2007 - 05:20 AM

So you want the user to enter one of those 3 codes correct?

So ABC111, DEF112 or XYZ113?

Shouldn't your ||'s be &&'s?

do
{
[...]
}while( (code is not ABC111) and (code is not DEF112) and (code is not XYZ113) );

This post has been edited by Glasseater: 18 June 2007 - 05:22 AM

Was This Post Helpful? 0
  • +
  • -

#3 Simply   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 07

Re: void function

Posted 18 June 2007 - 06:43 AM

I did try that and it does not make a differenc :unsure: :stupid: :blush:
Was This Post Helpful? 0
  • +
  • -

#4 PennyBoki   User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • Posts: 2,345
  • Joined: 11-December 06

Re: void function

Posted 18 June 2007 - 07:13 AM

try
while ((code == "ABC111" ) || (code == "DEF112" )|| (code == "XYZ113" ) || (code == "abc111" ) || (code == "def112" )|| (code == "xyz113" ));

Was This Post Helpful? 0
  • +
  • -

#5 Glasseater   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 15-June 07

Re: void function

Posted 18 June 2007 - 09:13 AM

View PostPennyBoki, on 18 Jun, 2007 - 07:13 AM, said:

try
while ((code == "ABC111" ) || (code == "DEF112" )|| (code == "XYZ113" ) || (code == "abc111" ) || (code == "def112" )|| (code == "xyz113" ));

That will do the same thing. The problem is that you need to be sure that NONE of those codes are entered, using ORs will only check if one of them is, or in this case, is not it.

e.g. If I were the computer running through the code and the user had entered ABC111 I would see this:

"ABC111" != "ABC111" (ok, we've found a false, if this were the only argument, we would exit, but there's more...) or "ABC111" != "DEF112" (oops! we've found a true, no exit...) etc...

I tried it with the code you posted and it does work (at least it doesn't get stuck in the loop).

The code should look something like this;
while(code != "ABC111" && code != "DEF112" && code != "XYZ113" && code != "abc111" && code != "def112" && code != "xyz113")

Was This Post Helpful? 0
  • +
  • -

#6 PennyBoki   User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • Posts: 2,345
  • Joined: 11-December 06

Re: void function

Posted 18 June 2007 - 09:43 AM

View PostGlasseater, on 18 Jun, 2007 - 09:13 AM, said:

View PostPennyBoki, on 18 Jun, 2007 - 07:13 AM, said:

try
while ((code == "ABC111" ) || (code == "DEF112" )|| (code == "XYZ113" ) || (code == "abc111" ) || (code == "def112" )|| (code == "xyz113" ));

That will do the same thing. The problem is that you need to be sure that NONE of those codes are entered, using ORs will only check if one of them is, or in this case, is not it.

e.g. If I were the computer running through the code and the user had entered ABC111 I would see this:

"ABC111" != "ABC111" (ok, we've found a false, if this were the only argument, we would exit, but there's more...) or "ABC111" != "DEF112" (oops! we've found a true, no exit...) etc...

I tried it with the code you posted and it does work (at least it doesn't get stuck in the loop).

The code should look something like this;
while(code != "ABC111" && code != "DEF112" && code != "XYZ113" && code != "abc111" && code != "def112" && code != "xyz113")








Since this is logic:
according to De Morgan's laws your code is same with mine.
It does the same thing.
Was This Post Helpful? 0
  • +
  • -

#7 jesh think   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-June 07

Re: void function

Posted 18 June 2007 - 10:13 AM

while ((code != "ABC111" ) || (code != "DEF112" )|| (code != "XYZ113" ) || (code != "abc111" ) || (code != "def112" )|| (code != "xyz113" )); //Condition of loop


i think the above should b replaced by
while (code != "ABC111" || "DEF112"|| "XYZ113" || "abc111" || "def112" || "xyz113" );[size=7]
reply if works
jesh think
:)
Was This Post Helpful? 0
  • +
  • -

#8 Simply   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 07

Re: void function

Posted 18 June 2007 - 11:23 AM

thank you PennyBoki it works beatuifully :)
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: void function

Posted 18 June 2007 - 12:01 PM

View Postjesh think, on 18 Jun, 2007 - 01:13 PM, said:

while ((code != "ABC111" ) || (code != "DEF112" )|| (code != "XYZ113" ) || (code != "abc111" ) || (code != "def112" )|| (code != "xyz113" )); //Condition of loop


i think the above should b replaced by
while (code != "ABC111" || "DEF112"|| "XYZ113" || "abc111" || "def112" || "xyz113" );[size=7]
reply if works
jesh think
:)

that syntax is not valid in c++.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1