C++Tutorial for Beginners 26 - Return Values: Getting Data From Subroutines

preview_player
Показать описание
-------------------------------------------------------------------------------------------------------------------------------------------
Return values let you get data out of your C++ functions.
--------------------------------------------------------------------------------------------------------------------------------------------
Рекомендации по теме
Комментарии
Автор

Nice video, trying to get a hang of it.

tokugeeky
Автор

Amazing tutorial ! Thank you for the help.

deionfarias
Автор

Hello,

This is great tutorial and John is doing a great job in educating people like me
about this wonderful language.

I had a small observation, at 2:43, the line //  int input = processSelection(); // is added. Here it might be useful to know that the function 'processSelection()' is first executed and then the returned value is stored in the variable 'input'. Thus you don't have to call the function twice, this was a mistake I was making, but now I figured this out.

Keep up the great work John! Appreciation and gratitude.

sankalptheamoebakid
Автор

Do you have any idea why the menu is showing twice? Sorry if it's a dumb question, maybe I just don't see it...

#include <iostream>
using namespace std;

int showMenu() {
int menuOption = 0;

cout << "Menu" << endl;
cout << "----" << endl;

cout << "Menu option 1 > Add a video." << endl;
cout << "Menu option 2 > Delete a video." << endl;
cout << "Menu option 3 > Search for a video." << endl;
cout << "Menu option 4 > View your video's." << endl;
cout << "Menu option 5 > Report a video." << endl;
cout << "Menu option 6 > Share a video." << endl;

cout << endl;
cout << "Select a menu option > " << flush;

return menuOption;
}

string passwordCheck() {
const string password = "1111";
string inputPassword = "0";

do {
const string password = passwordCheck();
string inputPassword = passwordCheck();

cin >> inputPassword;

if(inputPassword == password) {
cout << "Password correct, running selected menu option." << endl;
} else {
cout << "Password incorrect, try again > " << flush;
}
} while(inputPassword != password);

return password;
return inputPassword;
}

int main() {

showMenu();
int menuOption = showMenu();

do {
cin >> menuOption;

switch(menuOption) {
case 1:
cout << "To add a video, you need to enter your password > " << flush;
passwordCheck();
cout << "Adding..." << endl;
break;
case 2:
cout << "To delete a video, you need to enter your password > " << flush;
passwordCheck();
cout << "Deleting..." << endl;
break;
case 3:
cout << "Searching..." << endl;
break;
case 4:
cout << "Viewing..." << endl;
break;
case 5:
cout << "Reporting..." << endl;
break;
case 6:
cout << "Sharing..." << endl;
break;
default:
cout << "Unrecognized value, select a valid menu option > " << flush;
}
} while(menuOption > 6 || menuOption < 1);

return 0;
}

SyncNoMore
Автор

Is it typical in practice to give the parameter variable and the argument variable the same name? Or is there no particular convention?

bredmond
Автор

Hi John,
 your tutorials are incredible and are so easy to understand that i do not need to do any extra googling for help.
But this time I have two questions in my head and hope you can help me out.
Firstly, I am wondering if there is another way preventing the crash with using int at switching cases? The way I do is to change those int into char so most symbols are included hence not causing a crash. But is there another way of doing it? Like I cant avoid the repeats when i type in a sentence instead of one symbol.

 Secondly, the following program i wrote was working perfectly fine before splitting it into functions and add returning values. It keeps asking me to type my choice twice and i have no idea where did i get wrong at. Please have a look for me!

#include <iostream>
#include <string>


using namespace std;

void option_list(){
cout << "1. Sleep" << endl << "2. Eat" << endl << "3. Work" << endl;
}
int user_choice(){
char input;
cout << "Your Choice is: " << flush;
cin >> input;
return input;
}
void cases(){
char choice = user_choice();
switch (choice){
case 49:
cout << "You Are Going To Bed Now" << endl;
break;
case 50:
cout << "You Are Going To Kitchen Now" << endl;
break;
case 51:
cout << "You Are Going To Office Now" << endl;
break;
default:
cout << "Option invalid, Please Choose A Valid Option:" << endl;
}
}
int main() {
cout << "Please Choose An Option: " << endl;
while (true){
option_list();
user_choice();
cases();
cout << endl;
}
return 0;
}

Many Thanks,
Chris

chrisli
Автор

I tried to replicate this with a string however it prompts me twice before returning input. In other words:
Enter your name: John
Enter your name: John
Your name is John.

#include <iostream>
using namespace std;

string inputFunction(void);
void printFunction(void);

int main(void){
inputFunction();
printFunction();

return 0;
}

string inputFunction(void){
string name;

cout << "Enter your name: " << flush;
cin >> name;

return name;
}

void printFunction(void){
string name = inputFunction();

cout << "Your name is: " << name << "." << endl;
}

I am not sure why it prompts twice rather than just once?

KevinMThomas
Автор

I am playing now whit Arduino, and programing on PC is almost this same thing. I am surprising why Arduino peoples are mixing coding using some words from C,
like "serial.println" is in C as "printf", but compiling it on C++, where they could use more similar words to C++.
Also many peoples are teaching others to put in main program whole code, rather using functions or even use Classes and making one biblioteks.

maximo
Автор

Instead of declaring int selection = to the function, could you just switch the function? switch(processSelection())

PrjectConqur
Автор

I have this as my main function and everything works just fine except for the fact that 5 doesn't end the program. I know I can make the program end by adding another return 0 in the case of 5, but I want to know why the do while function doesn't end the code by itself when the input is 5. Can anyone help me out with that? Thanks in advance for any help and thanks to the creator of the video for having such a good set of videos!

#include <iostream>
using namespace std;

void showMenu(){
cout << "Please choose one of the following.\n\n";
cout << "1.\tCreate new document.\n";
cout << "2.\tDelete document.\n";
cout << "3.\tView document.\n";
cout << "4.\tSearch dDocument.\n";
cout << "5.\tQuit.\n";
}
int theProcess(){
cout << "Enter selection: " << endl;

int x;
cin >> x;

return x;
}
int main(){
int x = 0;
do{
showMenu();
int x = theProcess();

if(x == 1){
cout << "Creating new document...\n";
}
else if (x == 2){
cout <<"Deleting document...\n";
}
else if (x == 3){
cout <<"Viewing document...\n";
}
else if (x == 4){
cout <<"Searching document...\n";
}
else if (x == 5){
cout << endl;
return 0;
}
else cout <<"Unknown valuable. Please try again.\n";
}while(x!=5);
cout << "Quitting..." << flush;
return 0;
}

Nandinandito
Автор

So

int selection = processSelection();

Calls processSelection() method and also sets the whatever it returns to selection?

planetpeter
join shbcf.ru