so i need to know how to create a function that will display a message that prompts the user for a word or words(would be better). then returns that word, and only that word, to where ever the function is called. IDK what I'm doing wrong, but i think it wouldn't be a void function because its returning a string.. I'm extremely new so be gentile, any help would be so gratefully appreciated
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
string wordfunction(string);
string wordfunction(string wordonly)
{
cout<<"hi, whats the word you would like to enter?\n";
getline(cin, wordonly);//gets the sting or word or words
return wordonly;//i thought this would just return the string variable "wordonly"
}
int main()
{
wordfunction();//calls the function to display the message and get the word input
cout<<"heres another function using your word "<<wordfunction(wordonly);//supposed to just be the word of the first function
}
#include <iostream>
usingnamespace std;
string wordfunction(string);
string wordfunction(string wordonly)
{
cout<<"hi, whats the word you would like to enter?\n";
getline(cin, wordonly);//gets the sting or word or words
return wordonly;//i thought this would just return the string variable "wordonly"
}
int main()
{
wordfunction();//calls the function to display the message and get the word input
cout<<"heres another function using your word "<<wordfunction(wordonly);//supposed to just be the word of the first function
}
It should be :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
string wordfunction(string);
string wordfunction(string wordonly)
{
cout<< "Hi, whats the word you would like to enter? : ";
getline(cin, wordonly);//gets the sting or word or words
return wordonly;//i thought this would just return the string variable "wordonly"
}
int main()
{
string wordonly;
wordonly = wordfunction(wordonly);//calls the function to display the message and get the word input
cout<<"Here's another function using your word " << wordonly;//supposed to just be the word of the first function
}
Got it!, thank you guys so much i actually understand no and can now use this all over.
i have another question though. So i just sent in this code for school so its all said and done but i don't understand what i changed with the msgMulti function where it won't work anymore. So it displays the prompt asking the person for the message, but it doesn't take an input for the message. it skips that and goes to the input for the number of times the message is repeated. On the other hand, when i pull it by its self as its own program, it works just fine.
#include <iostream>
#include <cstring>
usingnamespace std;
void multiMsg()// reads a line and displays it an input amount of times
{
string msgToBeMulti;
int count, i;// int count for number of displays and i for the for loop
cout<<"What message would you like to be read?"<<endl<<"Message: ";
getline(cin, msgToBeMulti); //gets message
cout<<endl;//gets message
cout<<"How many times would you like it to be read?\nMultiplied By: ";
cin>>count;//times
for(i=0; i <= count ; i++)//for loop that stops displaying when i is incremented by 1 untill it is equal to count
{
cout<<msgToBeMulti<<endl;
}
}
int main()
{
multiMsg();
return 0;
}
So it displays the prompt asking the person for the message, but it doesn't take an input for the message. it skips that and goes to the input for the number of times the message is repeated.
You may need to use cin.ignore() if it is what you need. Actually my compiler has no issue with it.
#include <iostream>
#include <cstring>
#include <string>
usingnamespace std;
void multiMsg()// reads a line and displays it an input amount of times
{
string msgToBeMulti;
int count, i;// int count for number of displays and i for the for loop
cout<<"What message would you like to be read?"<<endl<<"Message: ";
cin.ignore(1000, '\n');
getline(cin, msgToBeMulti); //gets message
cout<<endl;//gets message
cout<<"How many times would you like it to be read?\nMultiplied By: ";
cin>>count;//times
for(i=0; i <= count ; i++)//for loop that stops displaying when i is incremented by 1 untill it is equal to count
{
cout<<msgToBeMulti<<endl;
}
}
int main()
{
multiMsg();
return 0;
}