jeudi 11 mai 2023

Whenever I run this code, score 1 gives some obscene number, and Im not sure how to fix it [closed]

I've tried to fix this issue by defining int score_1 and int score_2 in different forms, but It still doesn't work.

I update int score_1 to be 10 when running, and int score_2 to 5 while running, yet when I ask for display int score_1 gives an obscene number. I'm relatively new to this, so I'm sorry if this is an easy fix. I'm not sure if this is something can be done, or if this is something that requires numerous files.

I have attempted to give the variable an assigned integer (int_score1 = 0;) however this leads to my program assuming int_score1 is consistently 0.

#include <iostream>
#include <cmath>
#include <climits>
#include <float.h>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <array>
#include <fstream>
#include <memory>


using std::cout;
using std::cin;
using std::string;
using std::endl;
using std::hex;
using std::oct;
using std::array;
using std::ifstream;
using std::ofstream;
using std::vector;
using std::fstream;

void charades()
{
    int choice;
    int score_1;
    int score_2;
    cout << "Press 0 to quit" << endl;
    cout << "Press 1 to add a point to Team 1" << endl;
    cout<< "Press 2 to add a point to Team 2" << endl;
    cout << "Press 3 to display totals of Both Teams" << endl;
    cin >> choice;
    switch(choice)
    {
        case 0:
        cout << "Goodbye!" << endl;
        exit(0);
        break;
        case 1:
        score_1 += 1;
        break;
        case 2:
        score_2 +=1;
        break;
        case 3:
        if ( score_1>score_2)
        {
            cout << "Team 1 is beating Team 2 " << endl;
            cout << score_1<< " to " << score_2 << endl;
            //score 1 is giving long number
        };
        if (score_1< score_2)
        {
            cout << "Team 2 is beating Team 1 "<< endl;
            cout << score_2 << " To "<< score_1<< endl;
            //score 1 is giving long number
        };
        if(score_1==score_2)
        {
            cout<<"Both teams are tied at "<< score_1 << " !"<< endl;
            //works
        };
    
    
    
    }
};

int main()
{
    int choice;
    cout << "Would you like to score?\n 1. Charades" << endl;
    cin >> choice;
    do
    {
        switch(choice)
        {
            case 0:
            cout << "Goodbye!";
            exit(0);
            break;
            case 1:
            charades();
            break;
        }
    }
    while (choice !=0);

}

Aucun commentaire:

Enregistrer un commentaire