Beginner Program

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
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <Windows.h>


void lossfunc(int hero, int loss, int heroname)
{
	int healthremaining = hero - loss;
	std::cout << heroname << " has " << hero << " health, a Creeper hits him for " << loss << " health!";

}
	
void main()
{	
	int heroname;
		std::cout << "Type in the name you want your hero to have  =  ";
	std::cin >> heroname;
	int hero;
	std::cout << "Type in the amount of health you want our hero to have  =  ";
	std::cin >> hero;
	int loss;
	std::cout << "Type in the amount of health you want the hero to lose  =  ";
	std::cin >> loss;
	if (loss >= hero)
		std::cout << "You've killed" << heroname <<"!\n";
	lossfunc(hero, loss, heroname);
	_getch();


}


Hello.

When I choose hero's name, program skips to _getch.
int hero and int loss have some werid numbers like -889012931
The program is working propely if heroname is a value.

As you can probably see I am an begginer and I don't know your technical language, so please explain it to me in the simpliest way as possible.
this can't work, heroname is a integer, but you need a char* or a string, unless your hero's name is a number.
I didn't really know what char is, but I've changed int heroname to char heroname (in both lossfunc and main()), I think that was your point. However now, if heroname has more than 1 character in it, it returns the same strange numbers. Let's say I write AD in the first line where it asks me for the name, here's what happens:
It leaves int hero; and int loss; empty
In the line about the fight in lossfunc, it puts -858993460 in both hero and loss.
bump.
You should buy a proper book, such as the C++ Primer.
int variables only can store integers, not text. But all of that is explained in the book.
Last edited on
i think
here is all you need
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
#include"stdafx.h"
//#include <stdio.h>

#include <iostream>
//#include <conio.h>
//#include <Windows.h>
using namespace std;

void lossfunc(int hero, int loss,char *heroname)
{
	int healthremaining = hero - loss;
	cout << heroname << " has " << hero << " health, a Creeper hits him for " << loss << " health!"<<endl;;

}
	
void main()
{	
	char heroname[25];
	cout << "Type in the name you want your hero to have  =  ";
	cin.getline( heroname,20);
	int hero;
	cout << "Type in the amount of health you want our hero to have  =  ";
	cin >> hero;
	int loss;
	cout << "Type in the amount of health you want the hero to lose  =  ";
	cin >> loss;
	if (loss >= hero)
		cout << "You've killed" << heroname <<"!\n";
	lossfunc(hero, loss, heroname);
	//_getch();
	system("pause");


}

it run very well
good luck
and result
1
2
3
4
5
6
Type in the name you want your hero to have=  Nuc
Type in the amount of health you want our hero to have =90
Type in the amount of health you want the hero to lose  =100
You've killed Nuc!
 Nuc has   90  health, a Creeper hits him for  100  health!
Press any key to continue..... 



Last edited on
Okay, it works, thanks.
But why do I have to put [25] near char heroname in main()?

It isn't the max character number, so what is it? I also wrote 1 instead of 25, it were the same...

And why did you put cin.getline instead of cin >> ? It seems it's the same.
Don't use char arrays. You clearly dont' fully understand them yet.

It isn't the max character number, so what is it?


It IS the maximum character number. Actually the maximum character number is one less (because you need one for the null terminator. So for example if you have char heroname[25];, you can only have a name that is max 24 characters long.

Any longer and you corrupt memory which is bad bad bad.

By changing it to char heroname[1]; You basically are corrupting memory all the time (bad bad bad) unless the heroname is an empty string.

If you want a string, use a string:

1
2
3
4
5
6
7
8
9
#include <string>  // for string

...

string heroname;  // now heroname can be as long as you want.  No restriction.
//  no risk of memory corruption


getline(cin,heroname);  // get the heroname from the user 


strings are much safer and much easier to use. Don't use char arrays.

And why did you put cin.getline instead of cin >> ? It seems it's the same.


getline gets the whole line. >> stops at whitespace. So if the player inputs "Joe the Great" for their name, using >> will only get "Joe", whereas getline will get the full "Joe the Great".
1
2
3
std::string heroname;	
	std::cout << "Type in the name you want your hero to have  =  ";
		std::cin.getline(heroname, std::cout, std::cin);


Dot in std::cin.getline is underlined saying no instance of overloaded function, what's wrong?

Ouptut says something about char *, which isn't there...

1
2
3
4
5
6
7
8
9
  Lines.cpp
c:\documents and settings\pcdoctor\moje dokumenty\visual studio 2010\projects\firstagaincauseifail\firstagaincauseifail\lines.cpp(18): error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize,_Elem)' : cannot convert parameter 1 from 'std::istream' to 'char *'
          with
          [
              _Elem=char,
              _Traits=std::char_traits<char>
          ]
          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
before asking a question.you should read a book.
i think your questions are too easy,and i fell you a lazy people.if you don't read book,you won't understand correctly.
good luck
You need to work though the tutorials on this website. This program although very simple, appears to be beyond you. Work through the beginner tutorials:

Variables. Data types
Basic Input/Output

You'll be able to write it on your own then.

And i'd suggest using:

using namespace std;

underneath your includes like tuandt5 did. This will save you a whole lot of typing std:: at the beginning of each line.
Last edited on
std::cin.getline(heroname, std::cout, std::cin);

This is wrong. It should be this:

std::getline( std::cin, heroname );
Topic archived. No new replies allowed.