Need help with Input/Output/Functions

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:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

std::string get_string( std::string prompt )
{
    std::cout << prompt << "? " ;
    std::string str ;
    std::getline( std::cin, str ) ;
    return str ;
}

std::string get_name() { return get_string( "name" ) ; }

std::string get_email() { return get_string( "email" ) ; }

void print( std::string name, std::string email )
{
    std::cout << "My name is: " << name << '\n'
              << "My email is: " << email << '\n' ;
}

int main()
{
    const std::string name = get_name() ;
    const std::string email = get_email() ;
    print( name, email ) ;
}
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
}

http://coliru.stacked-crooked.com/a/0f0782fa0e98a705
http://rextester.com/YMVM69771
Topic archived. No new replies allowed.