0

I am learning C++ and I created a simple void function that uses char. I prototyped the function up top, defined it in the int main and tried to output "Your name is " x. Can someone tell me why it only tells me "Your name is" and not the x (john) part?

#include <iostream>
#include <cstdlib>
#include <cmath>

void myfun(char x);

int main() {
using namespace std;

char John;
myfun(John);

system("pause");
return 0;
}

void myfun(char x) {
using namespace std;

cout << "Your name is " << x << endl;
}

Wondering why this is getting downvoted...maybe I should stop trying to learn C++ becuase clearly no one wants to help anyone else learn

user1808010
  • 141
  • 1
  • 3
  • 11

3 Answers3

2

The main reason why it's not working is because you are not assigning a value to John. John is just the variable name that you use to identify with.

If you want to show Your name is John then you must give it a value and if you want it to print out a string it should be a String not a char.

Example:

#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

void myfun(string x);

int main() {
  string John = "John";
  myfun(John);

  system("pause");
  return 0;
}

void myfun(string x) {  
  cout << "Your name is " << x << endl;
}

Lastly, you don't need to put using namespace std into every function, you can simply put it on top or your code.

koralarts
  • 2,062
  • 4
  • 30
  • 48
  • 2
    Or you could stop using `using namespace std;` and prefix items defined in std with `std::`. Which is probably a better idea. See: [Why is “using namespace std;” considered bad practice?](http://stackoverflow.com/q/1452721/14065). If you ever use [Code Review](http://codereview.stackexchange.com/) this is one of the first things people will tell you. – Martin York Nov 09 '13 at 07:43
0

Edited code:

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

void myfun(std::string x);

int main() {

std::string name = "John";
myfun(name);

system("pause");
return 0;
}

void myfun(str::string x) {
cout << "Your name is " << x << endl;
}

Try this. to use string use std::string datatype.

Aakash
  • 1,860
  • 19
  • 30
  • I gave an example as to how char is used...downvote was not necessary...anyhow I edited the answer. – Aakash Nov 09 '13 at 06:54
0

It looks like the main problem with this is that you were under the impression that the variable John was initialized to "John". This is off for two reasons. First, the variable John is not initialized (It has not been assigned a value). Second, the variable John is a char which means it can only hold one character. Try changing char John; to this: string name = "John";

And then replace references to the variable.

One big problem with first time programmers is the assumption that the name of their variable has anything to do with the actual value that variable stores. I can do this: int doody = 3.14;

doody will have a decent estimation of pi. I can also do: int pi = 3.14;

and then pi would have a decent estimation of pi.

Just because you name a variable something does not mean it holds any data relevant to that name. Having said this you should stay away from naming variables things like 'x' and what not because that is not descriptive. The name of a variable should describe it's contents however a developer should be aware that the name of a variable does not determine it's content (yay ambiguity... sort of)

Teeknow
  • 1,015
  • 2
  • 17
  • 39