Hi! I've recently started my Program Structure & Design class and my class has been learning how to use C++. We're doing an assignment where we have to write a program that will ask questions and build a complete sentence. The sentences are:
My name is: __________
My email is: __________
He wants the program to be designed with functions so the code won't be all in one. This is what I have so far:
#include "stdafx.h"
#include <iostream>
#include <string>
usingnamespace std;
void name();
void email();
int main()
{
name();
email();
cout << "My name is: " << name << endl;
cout << "My email is: " << email << endl;
return 0;
}
void name()
{
cout << "My name is: " << endl;
string name;
getline(cin, name); name;
}
void email()
{
cout << "My email is: " << endl;
string email;
getline(cin, email); email;
}
However, when I try to build my code and answer the questions, it gives me an output of random letters and numbers (eg. My name is My name is: 00FD1668, My email is: 00FD10A5). If there's any solution to this, please let me know! And he didn't really teach us about adding more than one function so if there's any helpful tips on that, it'll help me lots. This code will probably look like a big mess and I'm sorry.
1) At lines 26 and 35, what is it you think those statements name; and email; at the ends of those lines are doing? Because they have no effect whatsoever.
2) Nowhere do you pass back the values of name and email from your functions to the calling code. JLBorges' post shows you how to do that.
3) In lines 13 - 14, name and email are the names of functions. When you use the name of a function like that, without parentheses, what you get is what we call a pointer to the function - that is, the address of the memory where those functions are stored. That's why you're getting strange numbers; those numbers are memory addresses.
> That's why you're getting strange numbers; those numbers are memory addresses.
This is non-conforming behaviour.
With a conforming implementation, there is no implicit conversion from pointer to function to pointer to const void The value that is printed would be 1 or true (result of implicit conversion to bool).
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
void name() {}
void email() {}
int main()
{
//
// warning: address of function 'name' will always evaluate to 'true'
std::cout << name << '\n' ; // 1
// warning: address of function 'email' will always evaluate to 'true'
std::cout << std::boolalpha << email << '\n' ; // true
}