I am doing homework for school, and basically when you enter "VIP"
, vip++
should be done. It won't even build when I put cin.getline
in while loop. getline
usually works in other codes but I don't understand it this time. Can someone help me?
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <cstring>
#include <cmath>
int main() {
int istok = 0, zapad = 0, loza = 0, vip = 0, mladi = 0;
cout << "Unesite I za istok, Z za zapad, L za lozu, VIP za VIP (rofl), M za "
"djecu i mlade."
<< endl;
char unos5;
while (unos5 != 'K') {
cin.getline(unos5, 5);
if (unos5 == 'I') {
istok++;
}
else if (unos5 == 'Z') {
zapad++;
}
else if (unos5 == 'L') {
loza++;
}
else if (unos5 == 'VIP') {
vip++;
}
else if (unos5 == 'M') {
mladi++;
}
/*else if(unos5 != 'I' || 'Z' || 'L' || 'VIP' || 'M'){
cout<<"Unjeli ste krivo! Pokusajte ponovno."<<endl;
cin>>unos5;
}*/
else if (unos5 == 'K') {
break;
}
}
cout << "Ukupna zarada od prodanih ulaznica je "
<< istok * 60 + zapad * 80 + loza * 100 + vip * 500 + mladi * 20
<< " kn." << endl;
return 0;
}
First of all cout
, cin
and endl
are in the std
namespace, so you need to either prepend std::
to them of add lines like using std::cout;
after the #include
s.
Regarding your code, the getline
member of istream
(the type of cin
) requires a char array, but you only define a char. I suggest not to use char arrays but use the free function getline
that gets a stream to read from as a parameter and that stores the result in a std::string
which allows safer and easier handling. As you did not use <cstring>
and getline
is in <string>
I changed that #include
.
Also your 'VIP' cannot do something useful. These are three characters in one, that cannot work. You need a string for it (double quotes, not single quotes).
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <cmath>
using std::cout;
using std::endl;
int main()
{
int istok = 0, zapad = 0, loza = 0, vip = 0, mladi = 0;
cout << "Unesite I za istok, Z za zapad, L za lozu, VIP za VIP (rofl), M za djecu i mlade." << endl;
std::string unos5;
while (unos5 != "K") {
std::getline(std::cin, unos5);
if (unos5 == "I") {
istok++;
}
else if (unos5 == "Z") {
zapad++;
}
else if (unos5 == "L") {
loza++;
}
else if (unos5 == "VIP") {
vip++;
}
else if (unos5 == "M") {
mladi++;
}
/*else if(unos5 != 'I' || 'Z' || 'L' || 'VIP' || 'M'){
cout<<"Unjeli ste krivo! Pokusajte ponovno."<<endl;
cin>>unos5;
}*/
else if (unos5 == "K") {
break;
}
}
cout << "Ukupna zarada od prodanih ulaznica je " << istok * 60 + zapad * 80 + loza * 100 + vip * 500 + mladi * 20 << " kn." << endl;
return 0;
}
Please also note that writing endl
has the effect of writing '\n' and then calling flush()
on the output stream. If you don't need the flush()
, then you should only write '\n'.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.