I am an absolute C++ novice it seems so i need some help.
I am trying to make a programm which determines the least amount of coins needed to pay for something.
Example: You tell it you want to pay for 2,50 so it says you need a 2€ coin and 0.50€ coin (instead of something like: you need 5 * 0.50€ coins)
So my approach was to give it the price so then it will run a while loop for each coin testing whether the coin still fits the Value.
My Problem is that i cannot change my integers at all and im pretty sure its some really simple mistake i just cant figure out.
Code (Just the Function that calculates):
int Rechner() {
int Betrag;
int ZweiEuro = 0;
int EinEuro = 0;
int FuenfzigCent = 0;
int ZwanzigCent = 0;
int ZehnCent = 0;
int FuenfCent = 0;
int ZweiCent = 0;
int EinCent = 0;
cin >> Betrag;
cout << "Die kleinse Menga an Muenzen um ihren Betrag zu bezahlen ist:" << endl << endl << endl;
ZweiEuro + 2;
cout << "2.00 = " << ZweiEuro << endl;
cout << "1.00 = " << EinEuro << endl;
cout << "0.50 = " << FuenfzigCent << endl;
cout << "0.20 = " << ZwanzigCent << endl;
cout << "0.10 = " << ZehnCent << endl;
cout << "0.05 = " << FuenfCent << endl;
cout << "0.02 = " << ZweiCent << endl;
cout << "0.01 = " << EinCent << endl << endl << endl << endl << endl << endl;
cout << "[Enter] druecken um zu beenden.";
cin.sync();
cin.get();
return 0;
So this doesnt have any while loops because they dont work just like the ZweiEuro + 2 doesnt change anything.
The Result always stays 0 whether I add something or not.
Im pretty sure i can finish this programm easily if i get behind changing the god damn values of my Variables.
Thanks for help.
Thats not how its done. Both of these should work:
ZweiEuro = ZweiEuro + 2;
ZweiEuro += 2;
Thats the way if you want to increment it.
You could also just assign it the 2 as long as you dont want to loop it or anything
ZweiEuro = 2;
You have to store the integer. By writing ZweiEuro + 2; you don to change ZweiEuro value. You should write ZweiEuro = ZweiEuro + 2; or ZweiEuro += 2; instead, which means that ZweiEuro would be ZweiEuro value plus two. This is just the syntax of c++. You will get used to it soon.
Related
I am a beginner in programming. I was doing some simple applications. I was already done with my programme when I realised that my variable is getting random, even if I set it to 0. I was trying to figure it out why. My goal is to add 1 to the "cor" variable when the answer is matching the random generated number, the "else" section is working as it has to be. Maybe someone more experienced can help me.
`
#include <iostream>
#include <string>
#include <time.h>
#include <Windows.h>
using namespace std;
int number;
int ynumber[5];
int cor = 0;
int falsed = 0;
int main()
{
cout << "Welcome to our lottery!" << endl;
cout << "We start in ..." << endl;
Sleep(2000);
for (int i = 3; i >= 0; i--)
{
system("cls");
cout << i << endl;
Sleep(1000);
}
system("cls");
srand(time(NULL));
for (int i = 0; i < 6; i++)
{
cout << "Type your " << i+1 << " number below" << endl;
cin >> ynumber[i];
number = rand() % 50 + 1;
cout << "The picked number is: " << number << endl;
Sleep(1000);
if (ynumber[i] == number)
{
cout << "Same same!" << endl;
cor = cor + 1;
}
else
{
cout << "Hope for better luck next time ;)" << endl;
falsed = falsed + 1;
}
}
system("cls");
cout << "Thank you for participating!" << endl << "Correct picked numbers: " << cor << endl << "Wrong picked numbers: " << falsed << endl << endl;
system("pause");
return 0;
}
`
int ynumber[5];
The length of your array is 5
for (int i = 0; i < 6; i++)
{
//...
cin >> ynumber[i];
You loop over the indices 0,1,2,3,4,5. Use your fingers to count the number of indices that you use. You'll notice that you access the array at 6 different fingers. 6 is more than 5. As such, we can conclude that you're accessing the array out of bounds. The consequence is that behaviour of your program is undefined.
Solution: Do not access an array out of bounds. The last index of array of length n is n - 1.
More generally: Don't rely on magic numbers. In this case, you could use instead:
for (int i = 0; i < std::size(ynumber); i++)
I have a double for loop going through a two-dimensional array of object classes, and there is supposed to be a repeated message every time it loops to give specific information, but the strings that are the same every time keep getting cut off at the front more each time it loops. What is the reason why it keeps doing this? I have never had this problem before while using C++.
System systems[29][119];
string ds[29][119];
int sx = 0;
int sy = 0;
for (int i = 0; i < 29; i++) {
for (int o = 0; o < 119; o++) {
int which = rand() % 4 + 1;
system("cls");
cout << "X: " + o << endl << "Y: " + i << endl << endl;
if (which == 1) {
systems[i][o] = genSystem(o, i);
cout << "ID: " + systems[i][o].id << endl
<< "Num Planets: " + systems[i][o].numPlanets << endl;
system("pause >nul");
}
else {
systems[i][o] = genSystem(o, i, false);
cout << "NO SYSTEM" << endl;
system("pause >nul");
}
}
}
You’re adding numbers to string literals (which represent arrrays), which ultimately boils down to pointer arithmetic.
It does not "stringify" the numbers and concatenate them as it would in some other languages.
"Hello" + 1 —> "ello"
"Hello" + 2 —> "llo"
And so on.
Use << instead of +.
You can separate your numbers in cout by << instead of using the arithmetic operator + to add an offset.
I am pretty new when it comes to programming with STL and I thought I was getting the hang of it. But I am a little perplexed about this one bit. My goal is to take in 5 values, then print out my values, print the highest value among them, print the average, and print the lowest among them ( my problem ). It seems that my variable "low" is given the value of 0 and I do not know why this is. I have tested to see if my values are being read in and to my knowledge, they are. So if anyone could please enlighten me to why I cannot seem to get the proper lowest value, I would greatly appreciate it. Thank you for your time.
vector<double> vecList;
int x = 0;
double high = 0;
double low = 0;
double sum = 0;
cout << "Enter Integer Values, then press Ctrl(z) to Quit:" << endl;
for (int i=0; i < 5; i++)
{
cin >> x;
sum = sum + x;
vecList.push_back(x);
}
vector<double>::iterator intVecIter;
cout <<"List contains: ";
for (intVecIter = vecList.begin(); intVecIter != vecList.end(); ++intVecIter)
cout << *intVecIter << " ";
for (int i=0; i < 5; i++)
{
if(vecList[i] > high)
{
high = vecList[i];
}
// prints out "0"
if(low > vecList[i])
{
low = vecList[i];
}
}
cout << endl << "Largest: "<< fixed << setprecision(2) << high << endl;
cout << "Smallest: "<< fixed << setprecision(2) << low << endl;
cout << "Average: " << fixed << setprecision(2)<< (sum/5);
return 0;
Since you are trying to learn STL, take a look at the algorithms library and it has some helper functions which will give the min, max and sum (accumulate is the actual function name) for a given range.
you need to initalize low to a big value not 0, otherwise this
if(low > vecList[i])
is never true
I need the user input to be saved into my array and then output the array before the user inputs the next time. I have been moving things around different ways but cannot seem to get them to perform properly. I tried to cut down the code to the two functions I am having issues with.
void PlayGame()
{
const int HighestNum = 50;
const int LowestNum = 1;
int RandomNumber = LowestNum + rand() % HighestNum; //set for better random results
cout << "Guess the random number between " << LowestNum << " and " << HighestNum << "!\n\n";
const int attempts = 15;// limits the attempts to guess the random number to 15
int Guess [attempts] = {};
cout << "Enter your guess " << endl;
for (int count = 0; count < attempts; count++)
{
cin >> Guess[count];
int z = RandomNumber, y = Guess[count], r;
r = reviewGuess (z,y);//calling the function that determines the results
switch (r)//switch statement for function results, letting the user know if they matched the number, if the number is higher, or lower
{
case 0:
cout << "You Win!!" << endl;
cout << "\n";
cin.get();
return;
case 1:
cout << "The number is higher than your guess" << endl;
break;
case -1:
cout << "The number is lower than your guess" <<endl;
break;
}
if (count == 15)
{
cout << "Sorry, no guesses remain. The random number was... " << RandomNumber << "!";//so the user can see the random number at the end of their attempts
cout << "\n";
cin.get();
Again();
}
}
return;
}
int DisplayGuess(int member[])
{
for(int i = 0; i < 15; ++i)
cout << "\nGuess " << i + 1 << ": " << member[i];
cout << endl;
return;
}
Try this inside your loop
if(count > 0)
{
for (int j= 0; j < count; j++)
{
cout<<Guess[j];
}
}
Call DisplayGuess() in the first line of the for loop. Since the first you time you call it your array is empty, it shouldn't output anything.
So,
for (int count = 0; count < attempts; count++)
{
DisplayGuess(Guess[count]);
cin >> Guess[count];
int z = RandomNumber, y = Guess[count], r;
r = reviewGuess (z,y);//calling the function that determines the
results
. . . . . .
You may have heard of a website called Project Euler (projecteuler.net). I'm working through the first problems, which were quite trivial, and I'm on the problem described in the title.
This isn't about optimising or anything - it takes about 90 thousandths of a second to complete. It's giving me the wrong total.
Can someone help me? I have no clue why the answer I'm getting - from both the array total (atotal) and the total that was added up normally (total) - is incorrect. The answer they are both showing is 947402457, which the website it telling me is the wrong answer.
In case it's just the wording, the question is here: http://projecteuler.net/index.php?section=problems&id=10
What's also very strange is, as far as I can tell, when, at the end when you can type in which prime number you would like to view (it takes it out of the array), it gives you the correct answer.
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>
typedef unsigned long int bignum;
//there are 666671 primes below two million
int main(){
using namespace std;
bignum top = 2000000;
bignum total = 0;
bignum atotal = 0;
//hardcode 2 and 3
total += 5;
int inc = 2;
bignum n = 5;
double sq = n;
bignum np = 1;
bignum *pa = new bignum[top];
pa[0] = 2;
pa[1] = 3;
while (n < top){
int div = 5;
int divinc = 2;
int p = 1;
//check if number is prime
//check divisiblity from any possible prime up to the square root of n
//first hardcode 2 and 3
if(n%2==0||n%3==0)
p = 0;
else{
while(div<=sqrt(sq)){
if(n%div==0){
p = 0;
break;
}else{
div = div + divinc;
if(divinc==2) divinc = 4; else divinc = 2;
}
}if(p!=0){ //if it's a prime - 0 is not, 1 is prime
total = total + n;
np++;
pa[np] = n;
//cout << np << " prime number: " << n << endl; //takes too long if it prints everything
}
}
n += inc;
if(inc==2) inc = 4; else inc = 2;
}
for (int c=0;c<=np;c++){
atotal += pa[c];
}
cout << "Total " << top << ": " << total << endl;
cout << "Array total: " << atotal << endl;
cout << "Elapsed time: " << clock() << " " << CLOCKS_PER_SEC << "s of a second" << endl << endl;
while(true){
int ptoview = 0;
cout << "Enter the number of the prime you would like to see (you can view every prime number below "<<top<<") ";
cin >> ptoview;
if (pa[ptoview-1]){
if (pa[ptoview-1] < top)
cout << ptoview << " prime: " << pa[ptoview-1] << endl;
else
cout << "Too high/low" << endl;
cout << endl;
}
}
system("PAUSE");
return 0;
}
Here's a clue to at least one problem. Have a look at what happens when you replace:
for (int c=0;c<=np;c++){
atotal += pa[c];
}
with:
for (int c=0;c<=np;c++){
bignum oldatotal = atotal;
atotal += pa[c];
if (atotal < oldatotal)
cout << "Hmmm: " << oldatotal << " " << atotal << endl;
}
I get something like:
Hmmm: 4294819625 12858
Hmmm: 4294864122 123849
Hmmm: 4294717053 27802
Hmmm: 4294697657 51420
: : :
Hmmm: 4293781002 792849
Hmmm: 4294658253 1676602
Hmmm: 4293686116 710941
Hmmm: 4294706293 1737578
Total 2000000: 947402457
Array total: 947402457
I won't go into the detail since this is a puzzle and I'm assuming you want to keep it at least a little challenging :-)
And yes, you're right (based on your comment below) so I'll make the answer a little less obtuse so it's more useful for others.
The unsigned long type is not big enough to hold the sum of all those primes so it's wrapping around.
Whether it can hold the actual primes themselves I haven't checked, but the answer in the next paragraph will solve that as well.
You might want to try redefining bignum as a "larger" type like unsigned long long if available.
Not looked at everything but sq isn't modified in the main while loop. That doesn't seem right. (BTW, I'd have used a sieve filter to get to the primes).