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