I have this section of code for generating an array of randomly generated and sorted ints, but the nosearch cin quits the program. Calling Cin.ignore() & cin.clear() before and/or after this line does not correct this, no error message is shown, the program just quits as soon as I hit enter on that section. This is the only statement to do this.
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include "search.hpp" //a function called sarch is here, program quits before sarch is called.
using namespace std;
int main () {
srand(time(0));
int tenn;
int minrange;
int maxrange;
cout << "Enter the number of integers: ";
cin >> tenn;
cout << endl << "Enter the lower limit: ";
cin >> minrange;
cout << endl << "Enter the upper limit: ";
cin >> maxrange;
int *arr = new int(tenn*10);
for (int i = 0; i < tenn*10; i++){
*(arr+i) = (rand() % maxrange) + minrange;
}
cout << "list, unsorted: " << endl;
for (int i = 0; i < tenn*10; i++){
cout << *(arr+i) << " ";
}
cout << endl;
for (int i = 0; i < tenn*10; i++){
int low = i;
for (int j = i; j < tenn*10; j++){
if (*(arr+j) < *(arr+low)){
low = j;
}
}
int temp = *(arr+i);
*(arr+i) = *(arr+low);
*(arr+low) = temp;
}
cout << "sorted list: " << endl;
for (int i = 0; i < tenn*10; i++){
cout << *(arr+i) << " ";
}
cout << endl;
int nosearch;
cout << "Enter the number of values you wish to search for: " << endl;
//The next cin will fail
cin >> nosearch;
cout << "v";
//more code here...
I use a makefile to compile the code, and when I run it in terminal, this is what happens:
>./Needle.exe
Enter the number of integers: 2
Enter the lower limit: 0
Enter the upper limit: 10
list, unsorted:
9 4 6 2 4 5 2 9 1 4 7 8 3 4 9 4 6 7 9 9
sorted list:
1 2 2 3 4 4 4 4 4 5 6 6 7 7 8 9 9 9 9 9
Enter the number of values you wish to search for:
3
>
I can only assume something I did above triggered this error, which is why I included so much of the code.
This question already has an answer here:
C++ - using enums to generate a deck of cards
(1 answer)
Closed 2 years ago.
it's in c++
The idea was to set it up so that it will keep looping through all the cards to keep getting different results and answers until all 52 cards are gone i dont know the exact placmet for it I know its
for(int i = 0; i < 5; i++)
{
cout << i << endl;
}
I wasn't quite sure how to set up the array If I just did it like string {ace, one two} and so on.. but I have the array labeled 52 even typing them all out its only 13 number repeated 4 times in 4 suits so would you class them all separately? Or is there something for that?
#include <iostream>
using namespace std;
class cardGame;
int main()
{
int bet;
int dealer[52] = { 13, 13, 13, 13 };
int player[52] = { 13, 13, 13, 13 };
int dealerCard1;
int dealerCard2;
int playerCard1;
int playerCard2;
int dealerTotal;
int playerTotal;
int shuffle;
cout << "This is My attempt at BlackJack" << endl;
cout << endl;
cin >> bet;
cout << "Player enter amount to bet: $ ";
cout << endl;
cin >> shuffle;
cardGame::playeer(playerCard1 + playerCard2 = playerTotal);
cardsGame.playerCard1 = 0;
cardsGame.playerCard2 = 0;
cardsGame.playerTotal = 0;
cout << "the First card is: " << cardsGame.playerCard1 = 0 << endl;
cout << "The second card is: " << cardsGame.playerCard = 0 << endl;
cout << "Your total is: " << cardsGame.playerTotal = 0 << endl;
cardGame::playeer(playerCard1 + playerCard2 = playerTotal);
if (playerCard1 + playerCard2 == 21)
{
cout << "WINNER WINNER CHICKEN DINNER!" << endl;
};
else (playerCard1 + playerCard2 > 21)
{
cout << "What a dissapointment you are to everyone!" << endl;
};
if (playerCard1 + playerCard2 > dealerTotal)
{
cout << "WINNER WINNER CHICKEN DINNER!" << endl;
};
else (playerCard1 + playerCard2 == dealerTotal)
{
cout << "What a dissapointment you are to everyone!"
}
cardGame::dealer(dealerCard1 + dealerCard2 = dealerTotal);
cardsGame.dealerCard1 = 0;
cardsGame.dealerCard2 = 0;
cardsGame.dealerTotal = 0;
cout << "the First card is: " << cardsGame.dealerCard1 = 0 << endl;
cout << "The second card is: " << cardsGame.dealerCard2 = 0 << endl;
cout << "Your total is: " << cardsGame.dealerTotal = 0 << endl;
cardGame::dealer(dealerCard1 + playerCard2 = playerTotal);
if (dealerCard1 + dealerCard2 == 21)
{
cout << "What a dissapointment you are to everyone!" << endl;
};
else (dealerCard1 + dealerCard2 > 21)
{
cout << "WINNER WINNER CHICKEN DINNER" << endl;
}
if (dealerCard1 + dealerCard2 > playerTotal)
{
cout << "What a dissapointment you are to everyone!" << cout endl:
};
else (dealerCard1 + dealerCard2 < playerTotal)
{
cout << "What a dissapointment you are to everyone!"
}
}
I don't know c++ (or blackjack); but below is an example in Javascript which illustrates some concepts and might push you in the right direction.
In terms of structure you might want something like:
// Set up your 'deck'
deck = {
hearts: [],
spades: [],
diamonds: [],
clubs: []
}
// The object 'deck' has four properties (hearts, spades...), each of which is an empty array.
// Use a loop to add your 'cards'
Object.keys(deck).forEach(suit => {
for (i = 1; i < 14; i++) {
deck[suit].push(i);
}
});
// For each property in the object 'deck', push ascending consecutive integers starting from the number 1 to each array, until the number 14 is reached.
This gives you:
deck = {
hearts: [1,2,3,4,5,6,7,8,9,10,11,12,13],
spades: [1,2,3,4,5,6,7,8,9,10,11,12,13],
diamonds: [1,2,3,4,5,6,7,8,9,10,11,12,13],
clubs: [1,2,3,4,5,6,7,8,9,10,11,12,13]
}
Attempting to create my first program Expected behaviour:
1. User inputs an integer (lets say 7)
2. User continues to input same integer more times (lets say 4 total)
3. User inputs 8
4. Program then prints ("the count of 7 was 4")
5. User continues to enter 8 (lets say a 8 was entered a total of 11 times)
6. User enters 73
7. Program then prints ("the count of 8 was 11")
After step 4, the program gets stuck and any inputs yeild "7 was entered 0 times"
#include <iostream>
int main(){
int inputtedvalue = 0;
int firstvalue = 0;
int cnt = 0;
if(std::cin >> firstvalue){
++cnt;
while(std::cin >> inputtedvalue){
if(inputtedvalue == firstvalue)
++cnt;
else{
std::cout << "the count of " << firstvalue <<" is " << cnt << std::endl;
inputtedvalue = firstvalue;
cnt= 1;
}
}
}
}
I was looking over your example, and i believe the following might get you back in the right direction.
using namespace std;
int main(){
int inputtedvalue = 0;
int firstvalue = 0;
int cnt = 0;
cout << "Enter Starting value."<< std::endl;
if (cin >> firstvalue){
cout << "New count " << firstvalue <<" of " << cnt << endl;
while(cin >> inputtedvalue)
{
if(inputtedvalue != firstvalue)
{
cout << "It Doesn't Match" << endl;
firstvalue = inputtedvalue;
}
else
{
cout << "It Matches" << endl;
}
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Here's a link to the problem in question: https://code.google.com/codejam/contest/6224486/dashboard#s=p1
Okay, so I got a bit hung up on this problem. Now that the quals are over, does anyone know why this wouldn't work? I've checked against about 50 different cases and haven't been able to find one that doesn't work. Here's my full code below.
Basically, looking for any cases where my algorithm could break.
//Includes
#include <iostream>
#include <fstream>
#include <list>
#include <algorithm>
#include <stdlib.h>
using namespace std;
//Functions
int solve(list<int>);
//Main
int main() {
int minutes = 0;
int numTestCases = 0;
int initNonEmpty = 0;
char tempChar;
int tempInt;
list<int> people;
//Import Data
//Get number of Test cases
ifstream infile;
infile.clear();
infile.open("B-small-attempt5.in");
//get NumTestCases
infile >> numTestCases;
int solution[numTestCases];
//Solve it
for(int i=0; i<numTestCases; i++){
//Reset vars
initNonEmpty=0;
infile >> initNonEmpty;
people.clear();
//Input data
for(int j=0; j<initNonEmpty; j++){
infile >> tempInt;
cout << tempInt;
people.push_back(tempInt);
}
cout << endl;
//Solve the set
people.sort();
people.reverse();
tempInt = solve(people);
cout << "Solve returns: " << tempInt << endl << endl;
solution[i] = tempInt;
}
//Output
ofstream outfile;
outfile.open("RealCase6.out");
for (int i=0; i < numTestCases; i++){
cout << "Case #" << i+1 << ": " << solution[i] << endl;
outfile << "Case #" << i+1 << ": " << solution[i] << endl;
}
}
int solve( list<int> data){
cout << "Starting Solve functions." << endl;
int tempMax;
int max =data.front();
int test=0;
cout << "Max: " << max << endl;
//Test if base case
if(max<=3){
cout << "Reached base case." << endl;
return max;
}
else if (max % 2 == 0 ){
cout << "Max is even" << endl;
tempMax = max/2;
data.pop_front();
data.push_back(tempMax);
data.push_back(tempMax);
data.sort();
data.reverse();
test=solve(data);
test=test+1;
cout << "test is:" << test << endl;
cout << "max is:" << max << endl;
if( test<=max){
return test;
} else {
return max;
}
}
else {
cout << "Max is odd" << endl;
tempMax = max/2;
data.pop_front();
data.push_back(tempMax);
data.push_back(tempMax+1);
data.sort();
data.reverse();
test=solve(data);
test=test+1;
cout << "test is:" << test << endl;
cout << "max is:" << max << endl;
if(test<=max){
return test;
} else {
return max;
}
}
}
And here's my input/output for 6 different cases.
https://www.dropbox.com/sh/55wq52lzuygd82s/AABYxJJ7zaeoMhgCmymJcwnAa?dl=0
I'll remove this if this is too early to start asking about the problems. Sorry about eh formatting, but I was trying to get this done quickly.
Edit1: Added Link to problem.
Edit2: Someone else found the error. I only thought to divide things into groups of 2, which gives a case of one person with 9 pancakes, where dividing it into a group of 6 and 3 and then 3 3 3 works best.
This breaks in cases where it's more efficient to divide into non-equivalent stacks. For example, the case
1
9
where one person has 9 pancakes. Dividing as evenly as possible into groups of 2 gives the solution:
min 1: 9
min 2: 5 4
min 3: 4 3
min 4: 3 2
min 5: 2 1
min 6: 1 0
min 7: 0 0
where as dividing unevenly gives the solution
min 1: 9
min 2: 3 6
min 3: 3 3 3
min 4: 2 2 2
min 5: 1 1 1
min 6: 0 0 0
which is more efficient.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string.h>
using namespace std;
int main()
{
char pin[6]; //character array to allow for ease of input
int randomNumbers[10]; //holding the randomized numbers that are printed back to the user
int pinStorage[5];
char pinVerify[5];
char pinReenter[6];
int result;
srand(time(0));
cout << "Enter your pin number" << endl;
cin >> pin;
for (int i = 0; i < 5; i++)
{
pinStorage[i] = pin[i] - '0';
}
for (int i = 0; i < 10; i++)
{
randomNumbers[i]= rand() % 3 + 1;
}
cout << "Pin Verify: ";
for (int b = 0; b < 5; b++)
{
for (int d = 0; d <10; d++)
{
if (pinStorage[b]== d)
{
pinVerify[b] = randomNumbers[d];
switch (pinVerify[b])
{
case 1: pinVerify[b] = '1';
break;
case 2: pinVerify[b] = '2';
break;
case 3: pinVerify[b] = '3';
}
cout << pinVerify[b] << " ";
}
}
}
cout << " " << endl;
cout << "Pin : 0 1 2 3 4 5 6 7 8 9" << endl;
cout << "Number: ";
for (int c = 0; c < 10; c++)
{
cout << randomNumbers[c] << " ";
}
cout << " " << endl;
cout << "Renter pin" << endl;
cin >> pinReenter;
for(int h = 0; h < 5; ++h)
{
int digit = pinStorage[h];
pinReenter[h] = randomNumbers[digit] + '0';
}
cout << "PV: " << pinVerify;
cout << "PR: " << pinReenter;
result = (strcmp(pinVerify, pinReenter));
switch(result)
{
case 1: cout << "You did not enter the pin correctly!" << endl;
break;
case 0: cout << "Pin Entered Correctly!" << endl;
break;
case -1: cout << "You did not enter the pin correctly!" << endl;
}
The above is my code. The objective is to be able to enter a normal 5 digit pin such as 12345. The program will then store that pin, and produce a screen like this:
Pin: 0 1 2 3 4 5 6 7 8 9
Num: 3 1 2 3 2 2 3 1 1 3
The program randomized the num part that you see and assigns it to the numbers above. That way the user reenters 12322 and the computer recognizes it as him entering the right code. The next time it where to do it, it would be rerandomized to something else. This whole project is designed to prevent shoulder surfing.
However I seem to be having a problem with my code. Almost everything is working but with the final strcmp function. Even though I have pinVerify set to 5, it still gives me 5 numbers and a ASCII character at the end, something like 21323☻. This makes it impossible to ever have pinVerify and pinReenter as equal, meaning it's impossible for the program to tell you you entered in the challenge correctly, even when you did.
Can anyone help me fix this? I've been looking and tinkering for a while and have been completely unsuccessful.
C-style strings are null-terminated. In your example, it means that, when you want to work with two strings of length 5, you should actually reserve 5+1=6 chars for each of them, and make sure that the 5-th (0-indexed) char contains a character with ASCII code 0. Otherwise, strcmp, and any other C-string function, proceeds past the end of your char[5] array until it finds a byte containing zero.