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;
}
}
}
Related
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]
}
I started to learn C++ and my homework is to write a code where you can enter 5 numbers and the program will tell you for each number whether it is a Fibonacci number or not.
I also tried using a do/while-loop in the isFibonacci function instead of the for-loop, but that did not fix the problem.
#include <iostream>
#include <cstdio>
using namespace std;
//function to test whether a number is a Fibonacci number or not
bool isFibonacci (int i)
{
//special cases with 0 and 1:
if ( i == 0 || i ==1) {
return true;
}
//for all other numbers:
int Fib1;
int Fib2;
int Fib3;
for (Fib3=0; Fib3>=i; Fib3++) {
Fib3 = Fib1 + Fib2;
Fib1 = Fib2;
Fib2 = Fib3;
if (Fib3==i){
return true;
}
else {
return false;
}
}
}
int main ()
{
bool result;
int numbers[5];
int i;
//asking for the 5 numbers
cout << "Please enter 5 numbers;" << endl;
cin >> numbers[0] >> numbers[1] >> numbers[2] >> numbers[3] >> numbers[4];
// giving back the result
for (i=0; i<5; i++) {
result=isFibonacci (numbers[i]);
if (result == true) {
cout << "Your number " << numbers[i] << " is a Fibonacci number!" << endl;
}
else {
cout << "Your number " << numbers[i] << " is not a Fibonacci number!" << endl;
}
}
return 0;
}
The first Fibonacci numbers are (0),1,1,2,3,5,8,12.
So when I enter 5 numbers, for example 1,2,3,4,5 I should get a "yes" for 1,2,3 and 5, but a "no" for 4.
However, my program claims that except for 1, none of these numbers are Fibonacci numbers.
Basically your approach was a good idea. But you made some implementation errors in your check function. Like not initialized variables and wrong calculations. And look at you for loop.
Additionally. There will be a problem with big numbers.
Many very smart people, explored the Fibonacci numbers. There are whole books available. Also a Wikipedia article. See here.
Or look into that book:
The(Fabulous) FIBONACCI Numbers by Alfred Posamentierand Ingmar Lehmann
Or also here on stackoverflow
Therefore I will not reinvent the wheel. Here is your revised software:
#include <iostream>
#include <cmath>
#include <numeric>
// Positive integer ? is a Fibonacci number
// If and only if one of 5?2 + 4 and 5?2 - 4 is a perfect square
// from The(Fabulous) FIBONACCI Numbers by Alfred Posamentierand Ingmar Lehmann
// Function to test whether a number is a Fibonacci number or not
bool isFibonacci(int w)
{
{
double x1 = 5 * std::pow(w, 2) + 4;
double x2 = 5 * std::pow(w, 2) - 4;
long x1_sqrt = static_cast<long>(std::sqrt(x1));
long x2_sqrt = static_cast<long>(std::sqrt(x2));
return (x1_sqrt * x1_sqrt == x1) || (x2_sqrt * x2_sqrt == x2);
}
}
int main()
{
bool result;
int numbers[5];
int i;
//asking for the 5 numbers
std::cout << "Please enter 5 numbers;" << std::endl;
std::cin >> numbers[0] >> numbers[1] >> numbers[2] >> numbers[3] >> numbers[4];
// giving back the result
for (i = 0; i < 5; i++) {
result = isFibonacci(numbers[i]);
if (result == true) {
std::cout << "Your number " << numbers[i] << " is a Fibonacci number!" << std::endl;
}
else {
std::cout << "Your number " << numbers[i] << " is not a Fibonacci number!" << std::endl;
}
}
return 0;
}
FizzBuzz program. The user enters numbers separated by a comma. The program reads input and lets the computer know if divisible by 3, 5 or both. When the user enters 15,5,30, the program will only output the first number, 15 and stops there. What am I doing wrong?
void processVector(vector<int> intVector)
{
bool loop;
for (int i = 0; i < intVector.size(); i++)
{
loop = true;
}
}
int main()
{
cout << "Welcome to the FizzBuzz program!" << endl;
cout << "This program will check if the number you enter is divisable by
3, 5, or both." << endl;
cout << "Please enter an array of numbers separated by a comma like so,
5,10,15" << endl;
cin >> userArray;
vector<int> loadVector(string inputString);
istringstream iss(userArray);
vector <int> v;
int i;
while (iss >> i);
{
v.push_back(i);
if (iss.peek() == ',')
iss.ignore();
if (i % 15 == 0)
{
cout << "Number " << i << " - FizzBuzz!" << endl;
}
else if (i % 3 == 0)
{
cout << "Number " << i << " Fizz!" << endl;
}
else if (i % 5 == 0)
{
cout << "Number " << i << " Buzz!" << endl;
}
else
{
cout << "Number entered is not divisable by 3 or 5." << endl;
}
}
system("pause");
}
Here is how I would approach the problem:
#include <iostream>
#include <string>
#include <vector>
int main() {
std::cout << "!!!Hello World!!!" << std::endl; // prints !!!Hello World!!!
std::cout << "Please enter your numbers seperated by a comma (5, 3, 5, 98, 278, 42): ";
std::string userString;
std::getline(std::cin, userString);
std::vector<int> numberV;
size_t j = 0; // beginning of number
for(size_t i = 0; i < userString.size(); i++){
if((userString[i] == ',') || (i == userString.size() -1)){ // could also use strncmp
numberV.push_back(std::stoi(userString.substr(j, i))); // stoi stands for string to int, and .substr(start, end) creates a new string at the start location and ending at the end location
j = i + 1;
}
}
for(size_t n = 0; n < numberV.size(); n++){
std::cout << numberV[n] << std::endl;
}
return(0);
}
This should give you a method to solve the problem (without handling the fizzbuzz part of your program) that I personally find simpler.
The basic form for using functions is:
<return type> <function_name(<inputs)>{
stuff
};
So, a basic function that takes a string and returns a vector (what you are wanting) would be:
std::vector myStringToVector(std::string inputString){
std::vector V;
// your code (see the prior example for one method of doing this)
return(V);
};
It also looks like they want a separate function for outputting your vector values, this could look something like:
void myVectorPrint(std::vector inputVector){
// your code (see prior example for a method of printing out a vector)
};
Thank you #Aaron for the help. Here is the finished code and it works great!
I had to take a little more time researching a few things and trying to understand which order and what to put where in terms of the functions and how to call them. I appreciate all the help as I said I am a noob.
#include "stdafx.h"
#include <iostream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;
vector<int> loadVector(string inputString)
{
stringstream ss(inputString);
vector <int> numberV;
int n;
size_t j = 0; // beginning of number
for (size_t n = 0; n < inputString.size(); n++)
{
if ((inputString[n] == ',') || (n == inputString.size() - 1))
{
numberV.push_back(std::stoi(inputString.substr(j, n)));
j = n + 1;
}
}
return numberV;
}
void processVector(vector<int> intVector)
{
for (int i = 0; i < intVector.size(); i++)
{
int n = intVector.at(i);
if (n % 15 == 0)
{
cout << "Number " << n << " - FizzBuzz!" << endl;
}
else if (n % 3 == 0)
{
cout << "Number " << n << " Fizz!" << endl;
}
else if (n % 5 == 0)
{
cout << "Number " << n << " Buzz!" << endl;
}
else
{
cout << "Number entered is not divisable by 3 or 5." << endl;
}
}
}
int main()
{
cout << "Welcome to the FizzBuzz program." << endl
<< "Please enter an array of numbers separated by comma's (5, 10, 15)"
<< endl;
string inputString;
getline(cin, inputString);
try
{
vector<int> intVector = loadVector(inputString);
processVector(intVector);
}
catch (const exception& e)
{
cout << "Exception caught: '" << e.what() << "'!;" << endl;
}
system("pause");
return 0;
}
I'm currently working on a program that outputs the number 1089 (i.e the Magic Number) of a three digit integer who's first digit is greater than its last. I have some code typed up, but am not receiving 1089, instead I'm receiving 891. Could anyone offer some explanation as to why.
//Uses a cout to inform user will be using the number 412 as an example.
//Uses a cout to explain to user the number needs to be reversed.
cout << "Alright, let's walk through an example, using the number 412." << endl;
int numExample = 412;
cout << "Please input 412" << endl;
cin >> numExample;
cout << "First, the number 412 is reversed." << endl;
//The following is done for reversing the number 412:
int reverseNum = 0, remainder = 0;
while (numExample)
{
remainder = numExample % 10;
reverseNum = (reverseNum * 10) + remainder;
numExample = numExample / 10;
}
cout << "The reverse of 412 is " << reverseNum << endl;
cout << "Next, we want to subtract the reverse of the original number from its reverse" << endl;
cout << "This gives us 198" << endl;
cout << "From there, we want to reverse 198." << endl;
//The same procedure is used to reverse 198
int numExample2 = 198;
cout << "Please enter 198" << endl;
cin >> numExample2;
int reverseNum2 = 0, remainder2 = 0;
while (numExample2)
{
remainder2 = numExample2 % 10;
reverseNum2 = (reverseNum2 * 10) + remainder2;
numExample2 = numExample2 / 10;
}
cout << "The reverse of 198 is " << reverseNum2 << endl;
int magicNumber = (reverseNum2 + numExample2);
cout << "Following that, we want to add 891 to 189 which gives us " << magicNumber << endl;
Try writing a function to handle this so your code is cleaner (It will also make it easier for people to help you!)
#include <iostream>
#include <cmath>
using namespace std;
int reverseDigit(int num); // For example purposes
int _tmain(int argc, _TCHAR* argv[])
{
int Number1,
Number2,
temp1,
temp2,
Result;
cout << "Enter the number 412: ";
cin >> Number1;
temp1 = reverseDigit(Number1);
temp1 = Number1 - temp1;
cout << "Enter the number 198: ";
cin >> Number2;
temp2 = reverseDigit(Number2);
Result = temp1 + temp2;
cout << "The magic number is: " << Result << endl;
return 0;
}
int reverseDigit(int num)
{
int reverseNum = 0;
bool isNegative = false;
if (num < 0)
{
num = -num;
isNegative = true;
}
while (num > 0)
{
reverseNum = reverseNum * 10 + num % 10;
num = num / 10;
}
if (isNegative)
{
reverseNum = -reverseNum;
}
return reverseNum;
}
I realize you're not working with negatives so you can remove that bit if you chose to use this, you can also expand on it... That being said this will make the actual " Reversing process " easier to work with and improve upon and read.
I am working on an exercise from my C++ book and I'm not sure how to fix it. I am supposed to get an int from the user and display the individual digits in the order they were entered. For instance 12345 would be displayed 1 2 3 4 5. 7365 would be displayed 7 3 6 5. I have most of the code written but there is a logical error and I can't figure it out. Here is my code:
int main()
{
int number = 0;
int digit = 0;
int temp = 0;
int counter = 0;
int sum = 0;
int divisor = 0;
cout << "Please enter a nonzero number.";
cin >> number;
cout << "\nThe number you entered was " << number;
// Determine the number of digits
temp = number;
while (temp != 0)
{
temp = temp / 10;
counter++;
}
cout << "\nThere are " << counter << " digits in your number.";
// Separate the digits
temp = number;
cout << "\nSeparating the digits\n";
do
{
divisor = (pow(10.0, --counter));
digit = temp / divisor;
temp = temp % divisor;
cout << digit << " ";
sum = sum + digit;
}
while (counter != 0);
cout << "\nThe sum of the number is " << sum;
return 0;
}
When I enter 5555 the output is 5560. When I enter 1234 the output is 1236. Can anyone help me find my error?
Here's one version:
// If the number is only one digit, print it.
// Otherwise, print all digits except the last, then print the last.
void digits(int x)
{
if (x < 10){
cout << x;
}
else{
digits(x / 10);
cout << " " << x % 10;
}
}
Thank you all for your help :-) Turns out my code works fine in another compiler so I guess it's just a netbeans glitch.