Jump to content
  • Advertisement
Sign in to follow this  
hothead

whats wrong with my code?

This topic is 5396 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

whats wrong with my code its not letting me put in any input its going straight to the end of the program
//a stupid little game

#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

int main()
{
    srand(time(0));
    int battle = (rand() % 2 + 1);
    char (won);
    char (lost);
    char(y);
    char fight = y;
        cout<<"you got into a big battle do want to fight(y/n)";
    if(fight == y)
        cout<<"\nyou"<<battle;
    if(battle == won)
        cout<<"won";
    if(battle == lost)
        cout<<"lost";
    cin>>fight;
    return 0;
}
    

Share this post


Link to post
Share on other sites
Advertisement
Your code is a mess.

You need to rewrite it :). Start over.

To clarify:

Y is never initialized to anything. Neither is won or lost for that matter. I didn't think that would even compile due to char() being a form of typecasting. And why are you asking for input at the end of the program when you're processing the data, which is supposed to come from the input, at the beginning?

Share this post


Link to post
Share on other sites
I'm not entirely sure, it might be because you're asking cin for a character, not a string. But theres some far worse problems there.
char(y);
char fight = y;

y is not initialized, so neither is fight, and neither won or lost are initialized either, so its meaningless to test against them. What you probably want is:
const int won = 0;
const int lost = 1;


And for testing against y, you probably want to test against 'y'. So your code should become:

#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

int main()
{
srand(time(0));
int battle = ((rand() % 2) + 1); // I'm fairly sure you need the extra brackets because
// + has a higher operator precedence than %
const int won = 0;
char fight = 'y';
cout<<"you got into a big battle do want to fight(y/n)";
if(fight == 'y')
cout<<"\nyou ";
if(battle == won)
cout<<"won";
else
cout<<"lost";
cin>>fight;
return 0;
}

Share this post


Link to post
Share on other sites
It's still not working here's what I have so far

//a stupid little game

#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

int main()
{
srand(time(0));
int battle = ((rand() % 2) + 1);
const int won =1;
const int lost =0;
char(y);
char(n);
int fight;
if(battle == won)
cout<<"won";
if(battle == lost)
cout<<"lost";
cout<<"you got into a big battle do want to fight(y/n)";
if(fight == 'y')
cout<<"\nyou"<<battle;
if(fight == 'n')
cout<<" goodbye";
cin>>fight;
return 0;
}


the error now is its not showing you won or you lost.

Share this post


Link to post
Share on other sites
Sorry, thats my bad I think. You want to set won to 1, and lost to 2. Not 0 and 1. You should really use an "else" statement instead of a second if, just because its good practice.

Share this post


Link to post
Share on other sites
Quote:
Original post by hothead
... its going straight to the end of the program*** Source Snippet Removed ***


If by 'go to the end of the program' you mean the input for the variable 'fight', it's doing that because you get this input at the end of the program. If you wanted to conditionally display 'won' or 'lost' based on the user input, you would need to put you cin statement before you test it. Right now it looks as though victory is based on what time it is. If you could give an idea of what you expected to come out, it would be easier to give more advice, but it's not clear what your intention is from the code. Keep trying. :)

Share this post


Link to post
Share on other sites
If you want it to say either "you won" or "you lost", then logically you must output "you" before "won/lost". Similarly, if you want to check the user's input, you need to get the input before you do the check.

Also, you should cin into a char variable, since you want to compare it to 'y' or 'n' which are characters. And the char variables y and n are no longer needed.

Hint: The *name* of a variable means *absolutely nothing* to the compiler (except that there are restrictions on what you can call it; to avoid confusing the compiler, you can't call it 'if' or 'for', for example). When you work with expressions (i.e. do arithmetic, check for equality etc.), it is working with the *contents* of the variables.

Share this post


Link to post
Share on other sites

//a stupid little game

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstring>

using namespace std;

int main()
{
srand(time(0));
int battle = (rand() % 2);
const int won = 1;
const int lost = 0;
char fight;
char battle[5];
if(battle == won)
strncpy(battle,"won",4);
else
strncpy(battle,"lost",4);
cout<<"You got into a big battle. Do want to fight? (y/n)";
cin>>fight;
if(fight == 'y')
cout<<"\nYou "<<battle;
if(fight == 'n')
cout<<"\nGoodbye";
return 0;
}

Share this post


Link to post
Share on other sites
well, i think you want to inquire if they wish to fight before you figure out if they won or lost ... right ?
so you'd need :

#include <iostream>
#include <cstdlib>

int main()
{
srand(time(0));
int battle = (rand() % 2) + 1;
char fight;
int won = 1, lost = 2;
std::cout << "You encountered badness. Do you wish to smash face ?" << std::endl; // display text
std::cin >> fight; // get input
std::cout << std::endl; // flush stream
/* decide what to do */
if( fight == 'y' || fight == 'Y')
{
/* calculate what happens in the battle */
if( battle == won)
{
std::cout << "You 1 the battle" << std::endl;
}
else if(battle == lost)
{
std::cout << "You missed, and got smacked." << std::endl;
}
}
else if( fight == 'n' || fight == 'N')
{
std::cout << "Brave Sir Robin ran away" << std::endl;
}

return 0;
}



<edit :: yeah, thats what gourgy did. i didn't read his.>

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!