d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Is This Poor Use Of Return Value?
Add Reply New Topic New Poll
Member
Posts: 268
Joined: Oct 29 2010
Gold: 25.00
Nov 17 2014 04:46pm
As the topic says; is this poor use of return(0)?

Code
#include <iostream>

using std::cout; using std::cin; using std::endl;

int main(){

//inputs a number seperated by space between 0 1 and 9 9
int first_num;
int second_num;
cout << "Enter number between 0 1 and 9 9: " ;
cin >> first_num;
cin >> second_num;

if (first_num == 1){

switch (second_num){

case 0:
cout << "Ten" << endl;
return(0);
case 1:
cout << "Eleven" << endl;
return(0);
case 2:
cout << "Twelve" << endl;
return(0);
case 3:
cout << "Thirteen" << endl;
return(0);
case 4:
cout << "Fourteen" << endl;
return(0);
case 5:
cout << "Fifteen" << endl;
return(0);
case 6:
cout << "Sixteen" << endl;
return(0);
case 7:
cout << "Seventeen" << endl;
return(0);
case 8:
cout << "Eighteen" << endl;
return(0);
case 9:
cout << "Nineteen" << endl;
return(0);
}
}

else {
switch (first_num){

case 0 :
cout << "" ;
break;
case 2 :
cout << "Twenty " ;
break;
case 3 :
cout << "Thirty " ;
break;
case 4 :
cout << "Fourty " ;
break;
case 5 :
cout << "Fifty " ;
break;
case 6 :
cout << "Sixty " ;
break;
case 7:
cout << "Seventy " ;
break;
case 8:
cout << "Eighty " ;
break;
case 9:
cout << "Ninety " ;
break;
default:
cout << "Invalid Number" << endl;
return(0); //Deals with cases where first number is > 9
}
}

switch (second_num){

case 0 :
cout << "" ;
break;
case 1:
cout << "One" << endl;
break;
case 2:
cout << "Two" << endl;
break;
case 3:
cout << "Three" << endl;
break;
case 4:
cout << "Four" << endl;
break;
case 5:
cout << "Five" << endl;
break;
case 6:
cout << "Six" << endl;
break;
case 7:
cout << "Seven" << endl;
break;
case 8:
cout << "Eight" << endl;
break;
case 9:
cout << "Nine" << endl;
break;
default:
cout << "Invalid Number" << endl;
return(0);//Deals with cases where second number is > 9
}
}


This post was edited by Kids on Nov 17 2014 04:47pm
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Nov 17 2014 06:30pm
None of that makes sense to me and the program doesn't run correctly. It always ends in an invalid choice. What exactly are you trying to do as there are a number of things you can change.

Quote (Kids @ 17 Nov 2014 17:46)
As the topic says; is this poor use of return(0)?

Code
#include <iostream>

using std::cout; using std::cin; using std::endl;

int main(){

//inputs a number seperated by space between 0 1 and 9 9
int first_num;
int second_num;
cout << "Enter number between 0 1 and 9 9: " ;
cin >> first_num;
cin >> second_num;

if (first_num == 1){

  switch (second_num){

  case 0:
  cout << "Ten" << endl;
  return(0);
  case 1:
  cout << "Eleven" << endl;
  return(0);
  case 2:
  cout << "Twelve" << endl;
  return(0);
  case 3:
  cout << "Thirteen" << endl;
  return(0);
  case 4:
  cout << "Fourteen" << endl;
  return(0);
  case 5:
  cout << "Fifteen" << endl;
  return(0);
  case 6:
  cout << "Sixteen" << endl;
  return(0);
  case 7:
  cout << "Seventeen" << endl;
  return(0);
  case 8:
  cout << "Eighteen" << endl;
  return(0);
  case 9:
  cout << "Nineteen" << endl;
  return(0);
  }
}

else {
  switch (first_num){
 
  case 0 :
  cout << "" ;
  break;
  case 2 :
  cout << "Twenty " ;
  break;
  case 3 :
  cout << "Thirty " ;
  break;
  case 4 :
  cout << "Fourty " ;
  break;
  case 5 :
  cout << "Fifty " ;
  break;
  case 6 :
  cout << "Sixty " ;
  break;
  case 7:
  cout << "Seventy " ;
  break;
  case 8:
  cout << "Eighty " ;
  break;
  case 9:
  cout << "Ninety " ;
  break;
  default:
  cout << "Invalid Number" << endl;
  return(0); //Deals with cases where first number is > 9
  }
}

switch (second_num){

case 0 :
  cout << "" ;
  break;
case 1:
  cout << "One" << endl;
  break;
case 2:
  cout << "Two" << endl;
  break;
case 3:
  cout << "Three" << endl;
  break;
case 4:
  cout << "Four" << endl;
  break;
case 5:
  cout << "Five" << endl;
  break;
case 6:
  cout << "Six" << endl;
  break;
case 7:
  cout << "Seven" << endl;
  break;
case 8:
  cout << "Eight" << endl;
  break;
case 9:
  cout << "Nine" << endl;
  break;
default:
  cout << "Invalid Number" << endl;
  return(0);//Deals with cases where second number is > 9
}
}


This post was edited by NinjaSushi2 on Nov 17 2014 06:30pm
Member
Posts: 268
Joined: Oct 29 2010
Gold: 25.00
Nov 17 2014 06:52pm
The program attempts to print the English word for a corresponding numerical value between 0 1 and 9 9. Make sure to input your value in that format (number SPACE number)
*If you enter it any other way it will tell you it is an "Invalid Number"

Ex: Enter number between 0 1 and 9 9: 2 1 (press enter)
Program says "Twenty One"
Exits...



This post was edited by Kids on Nov 17 2014 06:58pm
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Nov 17 2014 10:05pm
Why do you need a space between the numbers? Also if you want to cheat use.

Code
#include <whatever>

using namespace std;


Though it's more proper and correct to use std::whatever. Also return 0; is used to show a program has executed correctly.
0 = true ; 1 = false. Off/on yada yada yada.

I've can go on but I'm on my phone and it takes too long even with SwiftKey.

Edited 4 errors.

This post was edited by NinjaSushi2 on Nov 17 2014 10:08pm
Member
Posts: 268
Joined: Oct 29 2010
Gold: 25.00
Nov 17 2014 10:39pm
I'm just following the parameters set by the assignment... for whatever reason the instructor wants a space between the numbers.

I'm not entirely sure why return(0); made my debugger work but the program is working to the specifications of the assignment. I added it to avoid incorrect names returning when you enter something like "10 29"
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 17 2014 11:24pm
Quote (NinjaSushi2 @ Nov 17 2014 11:05pm)
Why do you need a space between the numbers? Also if you want to cheat use.

Code
#include <whatever>

using namespace std;


Though it's more proper and correct to use std::whatever.


Keep in mind that he is new, and will not run into the problem using directives can cause for quite some time. However, I do agree learning proper programming standards early is better. But overloading them with these things from the start can be cumbersome and disadvantageous to their overall improvement.


But while I have your attention, NinjaSushi2, do you understand why it is improper? I say this not to call you out, but for I noticed earlier you were having issues with a simple do while. It seems odd to me that you are bringing up concepts like this in his thread, when it would appear you are at the same level as him. So, for the sake of joint learning let's continue this discussion: do you understand what issues can arise with the using directive in larger software projects?

Quote
Also return 0; is used to show a program has executed correctly.
0 = true ; 1 = false. Off/on yada yada yada.


This is not entirely true. 0/1 are not for true and false. It is actually for EXIT_SUCCESS and EXIT_FAILURE which are defined in cstdlib, you can also return any other integer which would be user defined error codes. The reason it returns an int is so you can tell the user what the reasoning is behind your program closing, it was either successful, a failure, or perhaps something else.

This post was edited by Minkomonster on Nov 17 2014 11:44pm
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Nov 18 2014 04:41pm
Quote (Minkomonster @ 18 Nov 2014 00:24)
Keep in mind that he is new, and will not run into the problem using directives can cause for quite some time. However, I do agree learning proper programming standards early is better. But overloading them with these things from the start can be cumbersome and disadvantageous to their overall improvement.


But while I have your attention, NinjaSushi2, do you understand why it is improper? I say this not to call you out, but for I noticed earlier you were having issues with a simple do while. It seems odd to me that you are bringing up concepts like this in his thread, when it would appear you are at the same level as him. So, for the sake of joint learning let's continue this discussion: do you understand what issues can arise with the using directive in larger software projects?



This is not entirely true. 0/1 are not for true and false. It is actually for EXIT_SUCCESS and EXIT_FAILURE which are defined in cstdlib, you can also return any other integer which would be user defined error codes. The reason it returns an int is so you can tell the user what the reasoning is behind your program closing, it was either successful, a failure, or perhaps something else.


I don't know very much either but I try to help when I can. I've developed the idea that if I am incorrect in some way, someone will correct me. If it's above my head I just stay out and lurk the threads. As to your question:

I had wondered why I always saw std::(your code handle here) and figured it was carried over as good practice from C. Then I decided to Google it one day and found a much more logical reason that was written in a very elegant yet simplistic way. Found this on Stackflow. http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice



I usually try to Google my problems first. As for the do while. I was having a derp moment and didn't realize my close bracket was in an incorrect place. (Long day of school and studying. 0800-2100)

Quote
This is not entirely true. 0/1 are not for true and false. It is actually for EXIT_SUCCESS and EXIT_FAILURE which are defined in cstdlib, you can also return any other integer which would be user defined error codes. The reason it returns an int is so you can tell the user what the reasoning is behind your program closing, it was either successful, a failure, or perhaps something else.


Yes I agree. I am aware of the exit command. I like the return 0; because that was what I was first taught to determine the success of my program. When I see my zero I know that my entire code executed completely, though perhaps not always the expected outcome.



Please feel free to correct me anytime. I highly value your opinion and a few others.

Quote (New International Version)

Do not rebuke mockers or they will hate you; rebuke the wise and they will love you.
Member
Posts: 5,167
Joined: Nov 23 2006
Gold: 11.01
Nov 19 2014 09:55am
In my experience you don't want to ever return 0 or system.exit unless something very bad happens (although in testing it may be different).

Can't you use some sort of continue; or break; in a switch-case and make the user re-enter the number?


I haven't actually looked at the code as it's a wall of text but I did notice a switch-case when scrolling down.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll