Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm doing this in ***Visual Studios: Visual C++/win32/Win32 Console Application***
I'm aware that it looks more like C. I tried using the variables in my C++ book, But the program didn't read them. Is there a different #Include I should be using so it works? What should I be using instead of Int, Char, Printf, or scanf?

One of three things happen depending how I edit the code:

- I am able to enter in Player 1s input, but it skips over the rest and registers their names as numbers. So at the end it would say ***"0, your score was 14"*** instead of ***"JJ, your score was 14"***
- I am able to enter the first and second player names, but then it crashes.
- It goes straight to crashing as soon as I open it.

What I want to happen:

- Ask the user to input the names of 3 players.
- Assign a random score for each player
- Display, from highest to lowest score, the names and scores of the player.
(Player 3 your score 5, player 1 you scored 3, player 2 you scored 0)

Program does ask the question for 1 or 2 players before crashing. It can also display the score in order if it skips over asking the players their names.

Mind taking a look to see where I'm going wrong? Below is the code:


C++
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

char p1;
char p2;
char p3;
int p1n = rand() % 100 + 1;
int p2n = rand() % 100 + 1;
int p3n = rand() % 100 + 1;

char answer;


//PLAYER 1 Call: Displays name and score
//Code that prints players names and their scores goes here, it works so it's not included.

int main(){

    //Asks for Player Names
    printf("Player 1, Please enter your name: \n");
    scanf_s("%s", p1);
    printf("QUICK! Kill some things..\n");
    printf("PEW PEW PEW\n");
    printf("*Que dramatic blood splatter...*\n");
    printf("\n");

    printf("Player 2, Please enter your name: \n");
    scanf_s("%s", p2);
    printf("SAVE THE USELESS PRINCESS!\n");
    printf("Throw some uh Pizz ah.\n");
    printf("Move along, There's no violence in this game.\n");
    printf("Go sit on some turtles.\n");
    printf("\n");

    printf("Player 3, Please enter your name: \n");
    scanf_s("%s", p3);
    printf("Take control and go be a criminal.\n");
    printf("*Hint Hint*\n");
    printf("Run over some pedestrians.\n");
    printf("The Po-Pos hate when you run over pedestrians.\n");
    printf("\n");

    //This displays the results from highest to lowest
    //This is where the code would go, but it works so it won't be included.



    return 0;
}
Posted
Updated 18-Jan-15 7:58am
v3
Comments
Sergey Alexandrovich Kryukov 18-Jan-15 13:22pm    
Did you try to use the debugger? :-)
—SA
KeketZee 18-Jan-15 13:28pm    
Excellent Question. Yes. :-(
Sergey Alexandrovich Kryukov 18-Jan-15 14:17pm    
Excellent answer :-)
—SA

If you really want to use C-like code then you have to use character arrays for player names. And you have to always check the return value of scanf.

Using std::string together with std::cin, std::cout for I/O would greatly help you.
Moreover I would use a struct (or a class) for holding player details and then define an array of players, e.g.

C++
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

struct Player
{
  int score;
  string name;
};

int main()
{
  const int PLAYERS = 3;

  Player pl[PLAYERS];

  for (int n = 0; n <PLAYERS; ++n)
  {
    cout << "Plese enter player " << (n+1) << " name" << endl;
   // bla_bla_bla(n);
    cin >> pl[n].name;
    pl[n].score = rand() % 100 +1;
  }
}



That without even using OOP features.
 
Share this answer
 
Comments
KeketZee 18-Jan-15 13:52pm    
I adore you you sweet, nonjudgmental, wonderful, helpful person. My code works. And now I learned how to use cout << and cin >>

:D
Sergey Alexandrovich Kryukov 18-Jan-15 14:20pm    
I want to say that I really appreciate your understanding and responses to post on this page.
Especially after you answered me that you actually use the debugger. :-)
You're welcome to call back with your other questions.
—SA
CPallini 18-Jan-15 17:08pm    
Glad to help.
You are very welcome.
Sergey Alexandrovich Kryukov 18-Jan-15 14:18pm    
5ed
—SA
CPallini 18-Jan-15 17:08pm    
Thank you.
It's difficult to explain exactly what you are doing wrong without being able to see when your eyes start to glaze over...

You are declaring your player names as char
C#
char p1;
char p2;
char p3;
but then you use them as a string:

C++
//Asks for Player Names
printf("Player 1, Please enter your name: \n");
scanf_s("%s", p1);

The trouble is that a string is a collection of chars, so when you pass the player names to scanf_s it fulls more than one char with the name - lots of them if he enters "Death stalker the mighty!" for example.

The immediate problem would be solved by declaring the names as arrays of chars, but I think you would be better off going back to your course notes and reading them again - you should find a "better" c++ way to do things.
I can't tell you what that is: I don't know your course, or how far you have got and I don't want to introduce things your tutor hasn't mentioned and doesn't want to cover yet.
 
Share this answer
 
Comments
KeketZee 18-Jan-15 13:34pm    
That explanation was 10 times more helpful then stack or my class. :/ All stack did was insult me and I learned early on that I'm not going to get any help from my teacher. It's an online course, they tend to be less responsive...
OriginalGriff 18-Jan-15 13:46pm    
Try to ignore insults; it's easy for someone who has learned this stuff to assume that it's easy because they don't remember how hard it was when they first met it!

Or because they are a**holes of course...: laugh:

Either way, don't be rude back - that doesn't get anyone anywhere, it just escalates until you are shouting at each other in 200 point red capitals...
PIEBALDconsult 18-Jan-15 13:52pm    
" in 200 point red capitals"

Can I _do_ that? :D
OriginalGriff 18-Jan-15 14:10pm    
Why not?
*I* have... :blush:

It was "RTFM" if I remember correctly... It didn't end well.
KeketZee 18-Jan-15 13:54pm    
D: I don't. I deleted my post and found a new site. I like this one. They actually help. I used your post and the first post and my code works now. (You'd be amazed how much I do understand without getting glazed eyes.)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900