C++ :: How To Switch Between Boolean True / False Logic Within Do / While Loop

Mar 25, 2014

I have a hit a snag in a number guessing game program. I was given a half-completed program and told to use functions to complete the missing pieces. It looks unwieldy, but this is how it is supposed to be. I have temporarily made the random guess value visible for troubleshooting purposes.

#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int welcome() {
cout << " Welcome to the hi-low game!" << endl;

[code]....

The issue lies within this piece of code:

checkGuess(guess, correct); done = false; //either true or false
} while (!done);
cout << "Congratulations, you got it!" << endl;
return 0;
}

I need to manipulate the Boolean variable done so that it registers as false when the user inputs a number higher or lower than the randomly selected value. However, if the user guesses correctly, done will become true and the program will end.

As it stands now, the program will not terminate, and if I set done equal to true like so:

checkGuess(guess, correct); done = true; //either true or false
} while (!done);
cout << "Congratulations, you got it!" << endl;
return 0;
}

Every number the user inputs will register as correct instead of the one right guess.

View 4 Replies


ADVERTISEMENT

C++ :: True / False About Function Parameter

Sep 11, 2014

is it true or false

a function like void myfun(int num){} can receive type "int var" but can't receive type "const int var"

AND

a function like void myfun(const int num){} can receive both type "int var" and also type "const int var"

View 3 Replies View Related

C++ :: Boolean Function Not Returning False

Mar 24, 2014

I have a bool type function and set it to explicitly return false, but I am still getting true as the return.

#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]){
double coeffs[3];
double roots[2];

[code].....

View 7 Replies View Related

C++ :: Palindrome That Takes A Vector Parameter And Returns True Or False

Apr 26, 2014

Write a function palindrome that takes a vector parameter and returns true or false according to whether the vector does or does not read the same forward as backward (e.g., a vector containing 1, 2, 3, 2, 1 is a palindrome, but a vector containing 1, 2, 3, 4 is not).

Code :
#include <vector>
#include <iostream>
using namespace std;
void palindrome(vector<int>);

[Code] .....

View 4 Replies View Related

C :: Program That Grades A True / False Quiz - Data Available In Text File

Oct 14, 2014

You are to write a C program that grades a true-false quiz. The quiz data will be available in a text file; here is an example:

Quiz #8 (09/22/14) (corrected version)
TTFTTFTFTF
0080 TTFTTFTFTF
0340 TTFTFFTFTF

The first line in the data file is a comment that you may assume can be up to 80 charac- ters long; it cannot be blank. The second line contains the answer key for the 10-question true-false quiz. The following lines in the data file contain a student's id number in column 1 followed by their answers for the quiz in column 2. A 0 (zero) on a line by itself indicates that there are no more students to process.

Write a program that first reads in (from standard input; more on this in a moment) the comment line and answer key as strings followed by each student's data. Store the student's id numbers in an array. You are to "grade" each student's quiz using the key provided fol- lowed by recording their scores (in a second array) in order to be able to print them out later. You do not need to use an array of strings or characters in your program. Write your program allowing for up to 100 students, and you may assume that at least 1 student will have taken the quiz.

You should create test data text files and provide the data to your program using redirection from the command line (see the sample run below). Your program should output the number of students who took the quiz, the average score, the best score, and a table showing each student's id, score, and grade. The formatting, spacing, and labels in your output should 1 match the sample run below exactly.

Your program should determine each student's grade as follows: if the score is equal to the best score b or b−1, give an A. If it is b−2, award a B. Give C's for any score that is equal to b−3 or b−4, a D for b−5 and an F for all others.

Alright So I'm just stuck at comparing the key answer with student answer here is my code comparing one student's answer with the answer key . is that right ?? One more thing for some reason when i try to print answer[0] the result is nothing why is that

Code:
#include<stdio.h>
int main(void) {
char comment[80];
char answer [10];
char studentans [10];
int n=0,i=0,x=0;
int ID[10];

[Code] .....

View 1 Replies View Related

C++ :: How To Create True False And Multiple Choice Questions Using Text Files

Jan 7, 2015

So im trying to create a quiz using c++ that incorporates three different types of questions. These are the standard one answer question, true false questions and multiple choice questions.

How to create the true false and multiple choice questions?

One way that i came up with looks like this

Question A answer1 B answer2;

View 2 Replies View Related

C/C++ :: Inputting True For A Boolean Variable?

Feb 7, 2014

My assignment : "Input the answer as the words true or false and put the user's answer in a boolean alphabetic variable."

My code:

bool T;
cout << "Type 'true' for lowercase or 'false' for uppercase";
cin >> T;
if (T == "true") {
cout << "Enter a whole number in decimal base: ";
cin >> dec;
cout.setf(ios::showbase);
cout << "Decimal " << dec << " to Hexadecimal with lowercase: " << setbase(16) << dec;
}

I'm not sure how to get 'true' as user-input as true for bool.

View 3 Replies View Related

C++ :: Binary Search Function - Return True If Element Inputted By User Found In Array And False If Not

Nov 9, 2014

I was instructed to write a binary search function which would return true if an element, inputted by the user, was found in the array, and false if it was not. I'm not sure why, but my function always returns false. My code is as follows.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//binary search function
bool search (int array[], int item)

[Code] ....

Why my code does not fulfill it's purpose???

View 7 Replies View Related

C++ :: Recursive Boolean Function - Compare Two Stacks And Returns True If Identical

Oct 21, 2014

The question is to write a recursive boolean function that compares two stacks and returns true if they are identical. This is where I get stuck:

If the top items of both stacks are the same, the recursive call always returns true, because 'true' is saved on top of the return stack.

Here is my code:
template<class Type>
bool identicals(stackType<Type> s1, stackType<Type> s2) {
if(!s1.isEmptyStack() && !s2.isEmptyStack()) {
if(s1.top() != s2.top())

[Code] ....

View 2 Replies View Related

C++ :: How To Test Multiple Boolean Using A Switch Function

Oct 21, 2013

I don't know that a Function is the right word for switch/case but it seems like this would exist, is there a way to test multiple booleans using a switch function?

View 1 Replies View Related

C++ :: Mystic Logic Error In Double For Loop

Aug 16, 2014

In a record I have IDs of type int but some may repeat. These IDs also got sales of type float. I wish to compare the IDs number of times they appear and of match found accumulate their sales and put it in another struct. However if an ID is found which is new I pass it to a function to initiate sales..but my code never reach second ID, given I am trying with two IDs.. given IDs in transFile

ID Sale

12345 870
90909 100

I enter these two in transFile and match it with masterRecord. 12345 is matched and updated likewise but 90909 is never executed or matched in checkID function..

void sync_trans_files(unsigned short int count, master masterRecord[], char transFile[]){
const short int weeklyemployee=25;
//read number of records from transFile;
struct trans{

[Code].....

View 8 Replies View Related

C Sharp :: ForEach Loop Logic Using KeyValuePairs

Jun 3, 2013

I'm new to C#. I'm trying to create a program to audit some of our processors. As you will see below, I have two foreach loops that allow me access to errorOrders(returns <<UserName, #of errors>>) and totalOrders (returns <<UserName, #of totalOrders>>).

However, the problem i'm running into now is that it seems to constantly loop through these two ForEach loops. When i run it, the 'count' on both errorOrders & totalOrders is 38. The program loops through all 38 users just fine, but then it continues to loop through users again...re-doing the process that it just finished.

I'm looking to see if there is another way I could set this section up so that it only loops through users once. I was thinking about trying to create a 'Boolean flag' that would force the program out of the ForEach loop after it has run through all the users...but i'm sure there are other ways to handle this.

foreach (KeyValuePair<string, int> error in errorOrders)
{
foreach (KeyValuePair<string, int> total in totalOrders)
{  

[Code]....

I know i should probably seperate these two foreach loops instead of having them nested..however, the problem that arises once i do that is my errPercentage can not be calculated if i do.

View 1 Replies View Related

C# :: Infinite Loop Random Boolean Print Statement

Feb 19, 2014

I am working on a small simple program my program fills up a air plane with customers there is 2 levels 1 and 2 and it will put that person in the spot depending on being picked.

So far i made a boolean airplane with 10 spots I can enter 1 person into the airplane with no problem but then after he sits down i get a infinite loop? I assume i maybe have to use the continue statement I feel like there is something simple that im missing i want to add more people. here is what i have

class Program
{
static void Main(string[] args)
{ bool[] planeSeats = {false, false, false, false, false, false, false, false, false, false };

[Code]......

View 1 Replies View Related

C++ :: Create A Loop To Return Minimum Value Using Boolean Variable

Mar 20, 2013

I need to create a loop to return a minimum value using a boolean variable but don't quite understand how to format my algorithm.

#include <iostream>
using namespace std;
int main() {
bool first = true;
while (first) {
cout << "Enter a number (X to exit): ";
int num;

[Code]....

I know its jacked up any comments in the code would be great I need to finish the program but also understand it.

View 6 Replies View Related

C :: How To Make A Switch Case Loop After Input Entered

Dec 31, 2013

I'm working on a code that needs to loop a switch case back to the beginning after a certain input is entered.

ex) Code:

printf("Select an option);
printf("1. Play game");
printf("2. Status");
printf("3. Exit");
scanf("%i", &userinput);
switch(userinput);

[Code]...

For my program, I want option 2 to display something, then loop back to "select and option" after the user presses enter. How would I write that? Would i use a while? do while?

View 3 Replies View Related

C :: Switch Inside A While Loop - Write Min And Max To Get Negative Numbers

Nov 25, 2014

I got the while loop right... but how do i write the minimum and max to get negetive numbers like -88....

Code:
#include <stdio.h>#include <math.h>
#include <conio.h>
int gcd(int, int);
int main() {
int x, y, min, max, result, power, flag = 1;
char c;

[Code] ....

View 6 Replies View Related

C++ :: Using Rand Function In While Loop And Output Using Switch Statement

Jun 12, 2014

Basically I'm supposed to use a while loop to generate a random number and use a switch statement to output the appropriate information. I feel like I'm missing a few things that are very simple.

The errors are:
warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
warning C4700: uninitialized local variable 'randomNumber' used

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;
int main(){
int i = 0;
unsigned int randomNumber;

[Code] .....

View 2 Replies View Related

C :: Simple Multi-Session Calculator / Switch Case And While Loop

Jun 30, 2013

I have a problem with my simple operations calculator code (using C, in Code::Blocks). I am required to use a while loop statement so the user can execute multiple step operations without re-opening the program. When I launch the program, I get through the first session fine, but when I'm on the second session, when entering the two operands and press enter (to calculate), it just gives me the return and say press any key to continue (exit).Here is the code:

Code:

# include <stdio.h>
int main()
{
int num1, num2;
char op;
int finished = 0;
}

[code]....

View 8 Replies View Related

C++ :: For Loop - Code Puts Out Switch Statement 5 Times With Random Number

Jan 26, 2014

I am writing for loop with a switch so that scores can be inputted in by a judge. The issue that I am running into is that I will put out an the text then the test happens and the code puts out the switch statement 5 times with random number. Here is what I have written.

Code:
int main() {
int diver;
int option;
int Judge;
cout << "Enter Divers Name:";

[Code] ....

View 4 Replies View Related

C :: Use Switch Case As A Menu To Switch Between Patients

Dec 30, 2014

I am trying to make a program which manages the data of 5 different patients (its an uni assignment), and i want to use a switch case as a menu to switch between the patients. All other functions (as for example putting the infromation on a file) work, but i cant figure out to bring the switch to work. First it asks for the number of the patient which should be worked with, this works perfectly, but afterwards changiung between the persons doesnt work as thought. It should ask everytime after it switches to one patient (i removed some functions to make it easier to read) and then asks to which it should jump next. If i put the number of one case (lets say 3) it just stops the program.

********Example:
user@pc ~/wherever $ ./program
current variables:
jo = 2
a = 0
1 //the entered number
The variable a = 1
patient 1
Enter the number of the next patient2
// and then it closes
**************

The same thing happens if i compile an example code from a book, it writes the first case and then stops.

I looked already through the forum but didnt find a person with a similar problem, maybe i didnt dig deep enough.

I am running Linux Mint 17 and use gcc as compiler.

Code:

include <stdio.h>
#include <stdlib.h>
int a=0;
int jo=2;
int main(void){
printf("current variables:
jo = %d
a = %d

[Code]...

View 13 Replies View Related

C++ ::  if Statement Always False

Jun 26, 2013

i am trying to make a ticktacktoe game. I made a simple one with no ai but i decide to add an option to play against computer. And i code like that(i know this is really bad):

/*
Name: TickTackToeFinal
Copyright:
Author:
Date: 26.06.13 20:41
Description:
*/
#include <iostream>
#include <string>

[code].....

when i run the game its always play randomly(last if statements in bilgisayar functions.

not:bilgisayar means computer
zor means hard
orta means medium
kolay means easy
oyuncu means player
tur means round
oyun means game

View 7 Replies View Related

C++ :: Eliminating False Sharing With TBB

Nov 9, 2013

So, as the title says, I'm trying to eliminate false sharing, or, eliminate sharing writes between threads with TBB. The question is how.

Normally I'd make an array whose size is equal to the number of threads, then locally write to a local variable and update the array only at the end of the thread.

But, of course, I cannot seem to get either thread id or total number of threads TBB uses. I found a reference to tbb::enumerable_thread_specific, which as I understand, is supposed to work for exactly this. But as soon as I added it, it hurt performance by ~60% instead of making it better.

How to do this properly? You don't really need to look so hard on how the algorithm works (I don't know either). I know it's not quite right right now due to race conditions, but I'll fix that later. I used a reference implementation that I copied™, and my task is to parallelize it.

The parts where the problem is right now is in red (of course, it's not all problems; it's only a subset of them).

Code:
#include <iostream>
#include <vector>
#include <fstream>
#include <ctime>
#include <algorithm>

[Code] ....

View 11 Replies View Related

C/C++ :: Palindrome Function Only Return False?

Apr 29, 2015

I have a text file that consists of:

1457887541
Madam
Able was I ere I saw Elba
Straw? No, too stupid a fad. I put soot on warts.
Class is cancelled today

And all of them are returning false as a palindrome and am not sure why that is so.

bool isPalindrome(const char *input){
int first = 0;
int last = strlen(input) - 1;

//begin loop to compare first position with last
while(last > first){

[Code] .....

View 3 Replies View Related

C++ :: How To Differentiate False Statements In A Function

Apr 2, 2013

I'm havin trouble outputing different false statements in a boolean function... I'm currently working on a "secret number game" program which must generate a secret number and inform the user if his/her guess number is to high, to low or correct. I know boolean return true and false.. If the number is correct, the true statement will appear, if false... THAT'S where my problem starts cause now I have TWO statements to output..In a Function.. How do I make my program able to tell if the number guessed is "too high" or "too low" ?

View 6 Replies View Related

C# :: Logic For Building Numbers Pyramid?

Jan 7, 2012

Code:
1
121
12321
1234321
123454321 EDIT:
the above pyramid looks like a doom.i.e 1-5 will be in center.1-4(L)1-3(L)1-2(L)1(L) similarly 1-4(R) 1-3(R) 1-2(R) 1(R)

I'm partly successful in building the requirement of the program.but cant able to think the logic for the other half. how can i proceed.below is the code written in c#.

Code:
for (int i = 1; i <= 5; i++)
{
//int l = 1;

[Code]....

View 3 Replies View Related

C++ :: Logic Error - Maze Not Displaying

Jan 24, 2014

So I have a program where I solve a maze using stacks. So my display_maze function take two parameters and displays the maze according to where the x coordinate and y coordinate are.

I have an error during my move function, as my maze doesn't display for some reason

void Player::player_move() {
Stack stack;
Maze maze;
Lock taken;
// Monster monster;

stack.push_values();

[Code] ....

It should be displaying, because after debugging I discovered the problem isn't with the display_maze function.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved