Building a Toothpick Game of 23 in C++ - c++

I'm building a Toothpick Game of 23 in C++ as an homework assignment in my Programming course. I'm almost finished with the code and the output looks exactly like the one that I am suppose to follow.
I am supposed to use a function in my code but I don't know how to use the function. Everything in the program works like it should except the function returns 0 and that 0 is the last line of the output and that's the line that is not identical to the output I am supposed to follow. So maybe someone can help my find out how I can get this right.
#include <iostream>
using namespace std;
int computerMove(int numPicksLeft, int humanNumber);
int main()
{
int a, z=0, y=0;
a = computerMove(z, y);
cout << a;
return 0;
}
int computerMove(int numPicksLeft, int humanNumber) {
int number_left=23, n, cpu_turn;
do{
cout << "There are " << number_left << " toothpicks left. Pick 1, 2 or 3 toothpicks: ";
cin >> n;
if (n <= 3)
{
number_left -= n;
if (number_left > 4)
{
cpu_turn = (4 - n); // þar sem n er fjöldi tannstöngla dregnir af notanda.
cout << "I pick " << cpu_turn << " toothpicks" << endl;
number_left -= cpu_turn;
}
else if (number_left == 2)
{
cpu_turn = 1;
cout << "I pick " << cpu_turn << " toothpicks" << endl;
number_left -= cpu_turn;
}
else if (number_left == 3)
{
cpu_turn = 2;
cout << "I pick " << cpu_turn << " toothpicks" << endl;
number_left -= cpu_turn;
}
else if (number_left == 4)
{
cpu_turn = 3;
cout << "I pick " << cpu_turn << " toothpicks" << endl;
number_left -= cpu_turn;
}
else if (number_left == 1)
{
cpu_turn = 1;
cout << "I pick " << cpu_turn << " toothpicks" << endl;
cout << "You won!" << endl;
number_left -= cpu_turn;
}
else if (number_left == 0)
{
cpu_turn = 0;
cout << "I pick " << cpu_turn << " toothpicks" << endl;
cout << "I won!" << endl;
}
}
else
cout << "Invalid input. Try again." << endl;
} while (number_left > 0);
return 0;
}
I always get this 0 in the last line and I don't want that. So my question is. How can I use this function so it won't be like this?

The function signature indicates the return type of the function is an integer (int). The function itself will finally return 0 if it reaches the end which you assign and print in your main function. If you are not interested in the result of the function why have it return something at all?
You could change the return type to void and not return/assign anything and the 0 would be left out.
void computerMove(int numPicksLeft, int humanNumber) {
// Your code
// No return statement!
}
Something like this for example.
As a side note avoid using namespace std;.

Related

C++ Left Center Right Dice Game (Game Loop and connecting vectors/arrays to classes/header files)

So i have to code the dice game LCR for a final prject but am having some trouble. First off, I know the code is sloppy and really redundant, that's why im looking for help. I couldn't figure out how to connect the 'chips' int vector and 'name' array into the player.h file. I basically need help writing methods for the chip passing to make the code less redundant. But another problem of mine is having the game loop until just one person has chips. Thanks for any help or advice.
LeftCenterRight.cpp
#include <iostream>
#include <string>
#include <time.h>
#include "Dice.h"
#include "Player.h"
#include <vector>
#include <algorithm>
using namespace std;
void Player::gameRules()
{
cout << ("\n-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n\n");
cout << ("Left Center Right is a multiplayer dice game");
cout << ("With a minimum of three players.");
cout << ("Dice containing letters L, C, R along\n"
"with dots on the remaining side are rolled each turn");
cout << ("\n-----Each Player starts with three chips-----\n");
cout << (">> For each L rolled, the player must pass one chip to the player to their left\n"
">> For each R rolled, the player must pass one chip to the player to their right\n"
">> For each C rolled, the player must pass a chip into the center pot (out of play)\n"
">> Dots are neutral and require no action");
cout << ("If a player has three or more chips, he/she rolls all three dice\n"
"If a player only has two chips, he/she rolles onlt two dice\n"
"If a player only has one chip, he/she rolls only one die\n"
"If a player is out of chips, he/she is still in the game,\n"
"\tbut does not roll any dice and passes their turn"
"\n\n >>> The last player with chips is the winner <<<");
cout << ("\n\n-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n");
};
int main()
{
int result = 0;
int currPlayer = 1;
srand((unsigned)time(NULL));
int numPlayers;
cout << "How many players are playing? (Please enter 3 or more): " << endl;
cin >> numPlayers;
if (numPlayers <= 2)
{
while (numPlayers <= 2)
{
cout << "More players needed.";
cout << "How many players are player?: ";
cin >> numPlayers;
}
}
std::string* names = new string[numPlayers]; // getting names and index is seat number
for (int i = 0; i < numPlayers; i++)
{
cout << "Please enter your name player " << i+1 << endl;
cin >> names[i];
std::string playerName = names[i];
}
vector<int>chips[1]; // intial 3 chips
for (int i = 0; i < numPlayers; i++)
{
int InitialChips = 3;
chips->push_back(InitialChips);
}
Player::gameRules();
int sum = 0;
for (int i = 0; i < chips->size(); i++)
{
int num = chips->at(i);
sum+=num;
}
int thePot = 0;
int i = 0;
while (sum > 0)
{
if ( i >=4 )
{
i = 0;
}
string currPlayer = names[i];
int currSeat = i;
cout << "It's " << currPlayer << "'s Turn!" << endl;
cout << "[1] Roll Dice [2] Quit :";
int choice;
cin >> choice;
if (choice == 1)
{
if (chips->at(currSeat) == 0)
{
break;
}
if (chips->at(currSeat) >= 3)
{
for (int k = 0; k <= 3; k++)
{
int outcome = Dice::rollDice();
if (outcome == 1)
{
int currChips = chips->at(i);
chips->at(i) = (currChips - 1);
cout << ">> -1 chip to " << names[i] << endl;
cout << ">> " << names[i] << " now has " << chips->at(i) << " chip(s)" << endl;
if (i == 0)
{
int j = (numPlayers - 1);
int currChips2 = (chips->at(j));
chips->at(j) = (currChips2 + 1);
cout << ">> +1 chip to " << names[j] << endl;
cout << ">> " << names[j] << " now has " << chips->at(j) << " chip(s)" << endl;
break;
}
else
{
int j = i - 1;
int currChips2 = (chips->at(j));
chips->at(j) = (currChips2 + 1);
cout << ">> +1 chip to " << names[j] << endl;
cout << ">> " << names[j] << " now has " << chips->at(j) << " chip(s)" << endl;
break;
}
}
if (outcome == 2)
{
int currChips = chips->at(i);
chips->at(i) = (currChips - 1);
cout << ">> -1 chip to " << names[i] << endl;
cout << ">> " << names[i] << " now has " << chips->at(i) << " chip(s)" << endl;
if (i == chips->size())
{
int j = chips->at(0);
int currChips2 = chips->at(0);
chips->at(j) = (currChips2 + 1);
cout << ">> +1 chip to " << names[j] << endl;
cout << ">> " << names[j] << " now has " << chips->at(j) << " chip(s)" << endl;
break;
}
else
{
int j = i + 1;
int currChips2 = (chips->at(j));
chips->at(j) = (currChips2 + 1);
cout << ">> +1 chip to " << names[j] << endl;
cout << ">> " << names[j] << " now has " << chips->at(j) << " chip(s)" << endl;
break;
}
}
if (outcome == 3)
{
thePot++;
cout << ">> +1 chip to the Center Pot" << endl;
cout << "There are now " << thePot << " chip(s) in the Center Pot " << endl;
int currChips = chips->at(i);
chips->at(i) = (currChips - 1);
cout << ">> -1 chip to " << names[i] << endl;
cout << ">> " << names[i] << " now has " << chips->at(i) << " chip(s)" << endl;
break;
}
else if ((outcome == 4) || (outcome == 5) || (outcome == 6))
{
break;
}
}
}
// ^^basically copied and pasted most of the while loop for the other two numbers of dice to roll^^
// had redundant code for if the player had 2 chips, to roll two dice only ^^
// also redundant code for only one chip, to roll one die. ^^
}
}
else if (choice == 2)
{
break;
}
else
{
cout << ">> Input Error";
cout << "[1] Roll Dice [2] Quit";
cin >> choice;
}
i++;
}
return 0;
}
Dice.h
#pragma once
using namespace std;
class Dice
{
public:
static int rollDice();
static int diceOutcome;
};
int Dice::rollDice()
{
int diceOutcome;
diceOutcome = (rand() % 6) + 1;
switch (diceOutcome)
{
default:
cout << "Error, retry";
case 1:
if (diceOutcome == 1)
{
cout << " --- " << endl;
cout << "You rolled a | L | Move 1 chip left." << endl;
cout << " --- " << endl;
return 1;
}
break;
case 2:
if (diceOutcome == 2)
{
cout << " --- " << endl;
cout << "You rolled a | R | Move 1 chip right." << endl;
cout << " --- " << endl;
return 2;
}
break;
case 3:
if (diceOutcome == 3)
{
cout << " --- " << endl;
cout << "You rolled a | C | Move 1 chip to the center." << endl;
cout << " --- " << endl;
return 3;
}
break;
case 4:
if (diceOutcome == 4)
cout << " --- " << endl;
cout << "You rolled a | * | No action needed." << endl;
cout << " --- " << endl;
return 0;
break;
case 5:
if (diceOutcome == 5)
cout << " --- " << endl;
cout << "You rolled a | * | No action needed." << endl;
cout << " --- " << endl;
return 0;
break;
case 6:
if (diceOutcome == 6)
cout << " --- " << endl;
cout << "You rolled a | * | No action needed." << endl;
cout << " --- " << endl;
return 0;
break;
}
}
To be fair, I'm quite new to programming, but I have been working on this project for days and kept running into conatiner problems and storing the name and chip values independently. So i've tried a lot of different things, probably not correctly though so I'm open to anything.

Is there a way to minimise the amount of IF statements?

My code is full of If statements and I want to reduce the amount if not get rid of them all. How do I do that? Any other tips and improvements is much appreciated thanks!
I've just begun learning to code and have followed a tutrioal to write this but am struggling to understand how to minimise the amount of IFs.
void Combat() {
//combat simiulator
CombatHUD();
int userAttack;
int userDamage = 1000; //8 * level / 2;
int monsterAttack = 6 * monsterlevel / 2;
if (character.totalHealth >= 1 && monsterHealth >= 1) {
std::cout << "\n";
std::cout << "1. Attack\n";
std::cout << "2. Block\n";
std::cout << "3. Run\n";
std::cout << "\n";
std::cin >> userAttack;
if (userAttack == 1) {
//User Attack
std::cout << "Attacking... you did " << userDamage << " to the " << currentMonster << std::endl;
monsterHealth = monsterHealth - userDamage;
Sleep(1000);
CombatHUD();
if (monsterHealth >= 1) {
std::cout << "Monster is attacking... \n";
character.totalHealth = character.totalHealth - monsterAttack;
std::cout << "You recieved " << monsterAttack << " damage " << std::endl;
if (character.totalHealth <= 0) {
character.totalHealth = 0;
system("cls");
std::cout << "You died! Game over!";
Sleep(2000);
exit(0);
}
}
else if (monsterHealth <= 0) {
monsterHealth = 0;
if (character.level != character.maxlevel) {
character.current_xp += monsterXp;
LevelUp();
}
std::cout << "\n";
std::cout << "You defeated " << currentMonster << " you get " << monsterXp << "XP.\n";
Sleep(2000);
HUD();
}
Sleep(1000);
Combat();
}
else if (userAttack == 2) {
//User Block. broken?
std::cout << "Blocking\n";
int i = rand() % 100 + 1;
if (i >= 50) {
std::cout << "You blocked the incoming attack\n";
character.heal = character.level * 10 / 2;
std::cout << "you have been healed for " << character.heal << std::endl;
character.totalHealth += character.heal;
Sleep(1000);
Combat();
}
}
else if (userAttack == 3) {
//User escape
std::cout << "You try to run\n";
int x = rand() % 100 + 1;
if (x >= 50) {
std::cout << "You run away\n";
HUD();
}
else {
std::cout << "You failed to run away \n";
std::cout << "Monster does a critical hit! \n";
character.totalHealth -= monsterAttack + 10;
std::cout << "You suffered " << monsterAttack + 10 << "Your current health is " << character.totalHealth << std::endl;
Sleep(2000);
Combat();
}
}
else {
std::cout << "Invalid Input\n";
Sleep(500);
Movement();
}
}
}
void Movement() {
//user movement. enhance?
int choice;
std::cout << "\n\n";
std::cout << "1. Move forward\n";
std::cout << "2. Chill\n";
std::cout << "3. Move Backwards\n";
std::cout << "\n";
std::cin >> choice;
if (choice == 1) {
int temp = rand() % 100 + 1;
std::cout << "You begin moving forward...\n";
if (temp >= 50) {
Monster();
std::string tempName = monsterName[rand() % currentMonsterNames];
std::cout << "A " << tempName << "! Get ready to fight it!\n";
currentMonster = tempName;
Sleep(1000);
Combat();
}
std::cout << "You find nothing\n";
Sleep(1000);
HUD();
}
else if (choice == 2) {
std::cout << "You want to chill for the rest of the day\n";
if (character.totalHealth <= 99) {
character.totalHealth += 10 * character.level;
}
std::cout << "You healed by chilling Health is now " << character.totalHealth << std::endl;
Sleep(1000);
HUD();
}
else if (choice == 3) {
std::cout << "You begin moving backwards...\n";
std::cout << "You're going no where\n";
Sleep(2000);
system("cls");
}
else {
std::cout << "Invalid Input\n";
Sleep(500);
Movement();
}
}
void Monster() {
//monster creator
monsterHealth = 30; {
monsterlevel = (rand() % 3) + character.level;
}
monsterHealth = (rand() % 30) * monsterlevel;
monsterXp = monsterHealth;
if (monsterHealth == 0)
Monster();
if (monsterlevel == 0)
Monster();
}
void LevelUp() {
//level up mechanic
if (character.current_xp >= character.xp_to_level) {
character.xp_to_level += floor(character.level + 15 * pow(2, character.level / 7));
character.totalHealth = floor(character.totalHealth + 10 * pow(2, character.level / 8));
if (character.level >= character.minLevel && character.level <= character.maxlevel) {
character.level++;
}
else {
character.level = 5;
}
character.maxHealth = character.totalHealth;
std::cout << "Ba Da Bing! You've leveled up! You're max health has increased!" << std::endl;
Sleep(2000);
LevelUp();
}
Sleep(2000);
HUD();
}
Here's your Movement function represented as a switch:
void Movement() {
//user movement. enhance?
int choice;
std::cout << "\n\n";
std::cout << "1. Move forward\n";
std::cout << "2. Chill\n";
std::cout << "3. Move Backwards\n";
std::cout << "\n";
std::cin >> choice;
switch (choice)
{
case 1:
int temp = rand() % 100 + 1;
std::cout << "You begin moving forward...\n";
if (temp >= 50) {
Monster();
std::string tempName = monsterName[rand() % currentMonsterNames];
std::cout << "A " << tempName << "! Get ready to fight it!\n";
currentMonster = tempName;
Sleep(1000);
Combat();
}
std::cout << "You find nothing\n";
Sleep(1000);
HUD();
break;
case 2:
std::cout << "You want to chill for the rest of the day\n";
if (character.totalHealth <= 99) {
character.totalHealth += 10 * character.level;
}
std::cout << "You healed by chilling Health is now " << character.totalHealth << std::endl;
Sleep(1000);
HUD();
break;
case 3:
std::cout << "You begin moving backwards...\n";
std::cout << "You're going no where\n";
Sleep(2000);
system("cls");
break;
default:
std::cout << "Invalid Input\n";
Sleep(500);
Movement();
}
}
Learning switch statements and having even better function separation will usually reduce the amount of ifs. Ifs themselves are most likely unavoidable though.
This is an extremely broad, open-ended question for software design in general -- not just C++.
For the most part, you won't be able to remove ifs since you have conditional logic -- and this is fine. However there are design practices you can follow so that there are less if statements per function:
Factoring logic into smaller functions that isolate their concerns. For example, each of the different Combat actions could be individual functions. This doesn't reduce ifs, but limits the amount of nesting per-function to where it's logically needed.
Finding repeated code or patterns, and extracting them into functions. Again, this helps limit the ifs to where they are needed
Using a switch case in place of if/else ladder
With smaller functions, you can change if statements to check for early termination cases and return early to avoid nesting. This keeps nesting shorter, since the main branch after an early return is implicitly the else. For example:
void check_user_death()
{
// Instead of 'if (character.totalHealth <= 0)'
if (character.totalHealth > 0) {
return;
}
system("cls");
std::cout << "You died! Game over!";
Sleep(2000);
exit(0);
}
All of the above suggestions are just overall software design topics, and are not strictly-speaking specific to C++
As an unrelated note, you might want to look into using loops (e.g. for or while), since your Combat function seems to recursively call itself in a few cases -- which could potentially result in stack overflows.

Add a countdown timer to a math program quiz

I am trying to add a countdown timer to this program. I would like the timer to start when the first math fact question is asked and upon expiration i want the program to give the grade. What's the code to do this in c++ if possible?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
int main(int args, char* argv[])
{
int i;
int result;
int solution;
char fact;
bool done = false;
int correct = 0;
int count = 0;
do {
try {
cout << "Enter (m)ultiplication or "
<< "(a)ddition." << endl; /*or (s)ubstraction. */
cin >> fact;
while (!cin)
throw fact;
if (fact != 'A')
if (fact != 'a')
if (fact != 'M')
if (fact != 'm')
while (!cin)
throw fact;
cout << "Now, enter the number of the fact that
you would like to do." << endl;
cin >> i;
int wrong = 0;
int score = 0;
int j = 0;
while (!cin)
throw i;
switch (fact) {
case 'm':
case 'M':
while (j < 13) {
cout << "What's " << i << " x " << j << "?" << endl;
cin >> result;
while (!cin)
throw result;
solution = i * j;
if (result == solution) {
cout << "Great Job! That is the correct answer for the problem "
<< i << " x " << j << "." << endl;
cout << endl;
cout << endl;
cout << endl;
score++;
j++;
cout << endl;
}
if (result != solution) {
cout << "Oh no! " << result << " is NOT the correct answer for "
<< i << " x " << j << "." << endl;
wrong = wrong + 1;
count++;
}
if (count == 3) {
cout << "The correct answer is " << i * j << "." << endl;
j++;
wrong = wrong - 3;
count = 0;
}
if (count == 1) {
cout << endl;
count--;
wrong = wrong - 1;
}
if (count == 2) {
cout << endl;
count--;
wrong = wrong - 2;
}
}
case 'a':
case 'A':
while (j < 13) {
cout << "What's " << i << " + " << j << "?" << endl;
cin >> result;
while (!cin)
throw result;
solution = i + j;
if (result == solution) {
cout << "Great Job! That is the correct answer for the problem "
<< i << " + " << j << "." << endl;
cout << endl;
cout << endl;
cout << endl;
score++;
j++;
cout << endl;
}
if (result != solution) {
cout << "Oh no! " << result << " is NOT the correct answer for "
<< i << " + " << j << "." << endl;
wrong = wrong + 1;
count++;
}
if (count == 3) {
cout << "The correct answer is " << i + j << "." << endl;
j++;
wrong = wrong - 3;
count = 0;
}
if (count == 1) {
cout << endl;
count--;
wrong = wrong - 1;
}
if (count == 2) {
cout << endl;
count--;
wrong = wrong - 2;
}
}
if (j == 13) {
system("pause");
correct = score - wrong;
score = (correct * 100) / 13;
}
if (score >= 80) {
cout << "Excellent!!!!!" << endl;
cout << "You scored " << score << "%." << endl;
cout << "You got " << correct << " out of 13 correct." << endl;
cout << "Keep up the good work." << endl;
} else if (score >= 70) {
cout << "Congratulations!!!!!" << endl
cout << "You scored " << score << "%." << endl;
cout << "You got " << correct << " out of 13 correct." << endl;
cout << "Let's see if we can score even higher next time." << endl;
} else {
cout << "You scored below 70 which means that you may need some"
<< " more practice." << endl;
cout << "You scored " << score << "%." << endl;
cout << "You got " << correct << " out of 13 correct." << endl;
cout << "You might want to try the " << i << " facts again."
<< " Goodluck!!!!!" << endl;
}
}
} catch (char fact) {
cout << "Invalid input. You can only enter (m)ultiplication or"
<< " (a)ddition. Please try again." << endl;
cin.clear();
cin.ignore(100, '\n');
} catch (int i) {
cout << "Invalid input0. You can only enter a
number here. Please try again." << endl;
cin.clear();
cin.ignore(100, '\n');
} catch (...) {
cout << "Invalid input2. You can only enter a number here.
Please try again." << endl;
cin.clear();
cin.ignore(100, '\n');
}
} while (!done);
return 0;
}
The task is quite hard, but if you dare trying, I suggest doing it in two steps:
Implement inaccurate solution: timer expiration is checked between queries to user.
If there is some time left, next question is asked, otherwise statistics is shown. So program always waits for user input on the last question despite timer has run out. Not what exactly quizzes look like, but good move to start with.
Method: before starting quiz save current time, before each question take delta between saved time and current one and compare with time limit. Example with chrono (starting from C++11), example with oldschool clock
Add middle-question interruption
This part requires function, which will wait for user input not longer, than specified amount of time. So instead of using std::cin() you'll need to calculate amount of time left (time limit minus delta between cur time and start time) and call some sort of cin_with_timeout(time_left).
The hardest thing is implementing cin_with_timeout(), which requires solid knowledge of multithreading and thread synchronization. Great inspiration can be found here, but it is direction to start thinking rather than complete solution.

How do I close my program correctly in C++?

I am writing one of my first programs which reads from a file and allows you to play a game, I have been told the exit function is not a good idea.
I am trying to call back to main in order to close the program correctly but I get the following error:
C3861 'main': identifier not found.
any ideas where I went wrong or how I can properly call the main function?
Code Below:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
void extra() {
int lives = 3;
int UI, intAnswer;
int opt = 0;
string IN, NoQ, q, c1, c2, c3, Answer;
fstream quiz;
cout << "Welcome to the guessing game!" << endl;
quiz.open("QuizQuestions.txt");
getline(quiz, IN);
cout << "There are " << IN << " Questions" << endl;
while (quiz.good() && opt !=2) {
getline(quiz, q);
cout << "Question " << q << endl;
getline(quiz, c1);
cout << c1 << endl;
getline(quiz, c2);
cout << c2 << endl;
getline(quiz, c3);
cout << c3 << endl;
getline(quiz, Answer);
intAnswer = stoi(Answer);
cout << "What answer do you think it is? ";
cin >> UI;
if (UI == intAnswer) {
lives++;
cout << "You got it right! You now have " << lives << " lives left " << endl << endl;
//i = 0;
}
else {
cout << "You got the answer wrong sorry, the correct answer is " << Answer << endl;
lives--;
cout << "You now have " << lives << " lives" << endl;
//i = 0;
if (lives < 1) {
cout << "You lose, would you like to play again? 1 for yes, 2 for no? ";
cin >> opt;
if (opt = 1) {
cout << endl;
extra();
}
else if (opt = 2) {
quiz.close();
return;
}
}
}
}
quiz.close();
}
int main() {
int UI;
cout << "Would you like to do the quiz? 1 - yes other - no ";
cin >> UI;
if (UI = 1) {
extra();
}
return 0;
}
You can't call main yourself.
When you call a function and it gets to the end, the function pointer/flow will return to the calling code.
Let's consider the general structure of your code:
void extra() {
for (int i = 0; i = 1; i++) {
//^---I suspect you don't mean this, maybe i<1, or 3, or...
// recall == and -= are different
//snipped some details
if (UI == intAnswer) {
lives++;
cout << "You got it right! You now have " << lives << " lives left " << endl << endl;
i = 0;
}
else {
cout << "You got the answer wrong sorry, the correct answer is " << Answer << endl;
lives--;
cout << "You now have " << lives << " lives" << endl;
i = 0;
if (lives < 1) {
cout << "You lose, would you like to play again? 1 for yes, 2 for no? ";
cin >> UI;
if (UI = 1) {
cout << endl;
extra();
//^--- I suspect you don't need this recursive call
}
else {
quiz.close();
return;
// ^---- return back to where we started
}
}
}
}
}
quiz.close();
system("pause");
}
int main() {
int UI;
cout << "Would you like to do the quiz? 1 - yes other - no ";
cin >> UI;
if (UI = 1) {
extra();//we come back here after the function stops
}
return 0;
}
Note I have simply put return where you want to end the function/program.
Instead of calling main, you can simply return from the extra function. The program then continues execution from where you called extra.
Just return to main.
else {
quiz.close();
𝙧𝙚𝙩𝙪𝙧𝙣;
}

Empty string position to return as *

Very new to programming.
This bit of my program accepts two strand of DNA as input and output them in a double helix drawing. The problem is, if one of the two input strand is longer than the other, i will receive error.
So I thought, is it possible that if strand[add] is non-existent anymore, replace it with *?
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
void helix(string &strand1, string &strand2)
{
int nucleo;
int length;
if (strand1.length() >= strand2.length())
{
length = strand1.length();
}
else
{
length = strand2.length();
}
int add;
for (int add = 0; add <= length - 1; add++)
{
if (add > 7)
{
nucleo = add % 8;
}
else
{
nucleo = add;
}
if (nucleo == 0)
{
cout << " " << strand1[add] << "---"<<strand2[add] << endl;
}
else if (nucleo == 1)
{
cout << " " << strand1[add] << "------" << strand2[add] << endl;
}
else if (nucleo == 2)
{
cout << " " << strand1[add] << "------" << strand2[add] << endl;
}
else if (nucleo == 3)
{
cout << " " << strand1[add] << "---" << strand2[add] << endl;
cout << " *" << endl;
}
else if (nucleo == 4)
{
cout << " " << strand2[add]<<"---" << strand1[add] << endl;
}
else if (nucleo == 5)
{
cout << " " << strand2[add]<<"------" << strand1[add] << endl;
}
else if (nucleo == 6)
{
cout << " " << strand2[add]<<"------" << strand1[add] << endl;
}
else if (nucleo == 7)
{
cout << " " << strand2[add]<<"-----" << strand1[add] << endl;
cout << " *" << endl;
}
}
}
int main()
{
string strand1,strand2;
cout << "ENTER STRAND:" << endl;
cin >> strand1;
cout << "ENTER STRAND:" << endl;
cin >> strand2;
helix(strand1,strand2);
_getch();
return 0;
}
I was hoping I could still show the longer strand even if the other side of the strand is empty(want to put *) like this :imgur.com/t7riVrS
I think you inverted the legnth test, it should be:
//if (strand1.length() >= strand2.length())
if (strand1.length() < strand2.length())
{
length = strand1.length();
}
else
{
length = strand2.length();
}
Edit:
If you want it fill one the string with '*', replace the code above with:
while (strand1.length() < strand2.length())
{
strand1 += "*";
}
while (strand1.length() > strand2.length())
{
strand2 += "*";
}