ERROR CODE ABOUT STRUCT array, shows 0 on the output [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I've made a code about Bank card, does it want silver or gold, then calculate the saving interests in 12 months,The output should be 112682512 if we input the saldoAwal = 100000000, but somehow it shows 101000000
What should I do?
I'm so confused because I've checked the calculations and still wrong, Help me?
THANKS
#include <iostream>
#include <iomanip>
using namespace std;
struct Orang
{
string namaNasabah = "";
int jenisTabungan = 0;
int saldoAwal = 0;
int saldoBaru = 0;
};
Orang orang[100];
int n, i = 0;
float saldoMin, bunga, biayaAdministrasi;
void bungaTahunan(Orang orang[100]);
int main()
{
cout << "Banyak nasabah : ";
cin >> n;
for(int i = 0; i < n ; i++)
{
cout << "Nama nasabah : ";
cin >> orang[i].namaNasabah;
cout << "Pilih silver atau gold? (1/2)"<<endl;
cin >> orang[i].jenisTabungan;
cout << "Saldo awal anda : ";
cin >> orang[i].saldoAwal;
bungaTahunan(&orang[i]);
}
cout << "nomor" << setw(20) << "nama" << setw(20) << "jenis tabungan" << setw(20) << "saldo awal" << setw(20) << "saldo akhir" << endl;
for(int i = 0; i < n ; i++)
{
cout <<setprecision(10)<< i+1 << setw(20) << orang[i].namaNasabah << setw(20) << orang[i].jenisTabungan << setw(20) << orang[i].saldoAwal << setw(20) << orang[i].saldoBaru << endl;
}
return 0;
}
void bungaTahunan(Orang orang[100])
{
if(orang[i].jenisTabungan == 1)
{
saldoMin = 10000000;
bunga = 0.01;
biayaAdministrasi = 10000;
}
else if (orang[i].jenisTabungan == 2)
{
saldoMin = 50000000;
bunga = 0.02;
biayaAdministrasi = 25000;
}
orang[i].saldoBaru = 0;
for(int i = 0; i<12; i++)
{
if(orang[i].saldoAwal < saldoMin)
{
orang[i].saldoBaru += orang[i].saldoAwal + (orang[i].saldoAwal * bunga) - biayaAdministrasi;
}
else
{
orang[i].saldoBaru += orang[i].saldoAwal + (orang[i].saldoAwal * bunga);
}
}
}
Sorry if its in Indonesian Language.

Last lines of your code is:
orang[i].saldoBaru = 0;
...
orang[i].saldoBaru = orang[i].saldoBaru + (orang[i].saldoBaru * orang[i].bunga);
saldoBaru is always 0, because "0 + 0*x" is 0

Related

why inside of loops not processing? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
i know Matlab as a beginner. It works in Matlab but not working in c++.
#include <iostream>
using namespace std;
int main()
{
int birinci, degerbir, sonuc, degeriki, degeruc, degerdort, degerbes;
int opr = 144;
int basamaksayisi;
int i;
cout << "kac basamakli olsun" << endl;
cin >> basamaksayisi;
while (basamaksayisi != i)
{
cout << "birinci degeri giriniz";
cin >> degerbir;
cout << "deger 1:" << degerbir << endl;
sonuc = degerbir;
i = 1;
cout << i;
cout << "ikinci deger giriniz";
cin >> degeriki;
degeriki = degeriki * 10;
sonuc = degerbir + degeriki;
i = 2;
cout << i;
cout << "ucuncu deger giriniz";
cin >> degeruc;
degeruc = degeruc * 100;
sonuc = degeruc + sonuc;
i = 3;
cout << i;
cout << "4.deger giriniz";
cin >> degerdort;
degerdort = degerdort * 1000;
sonuc = degerdort + sonuc;
i = 4;
cout << i;
cout << "5.deger giriniz";
cin >> degerbes;
degerbes = degerbes * 10000;
sonuc = degerbes + sonuc;
i = 5;
cout << i;
}
cout << "degerler sunlardır:" << degerbir << endl << degeriki << endl << degeruc << endl << degerdort << endl << degerbes << endl;
cout << "degeler toplami" << sonuc;
return 0;
}
why this "i" and other math operations etc. not counting in while loop ? also similar if else loop not working. Just in and count functions working.
i was tried change while loop to if loop. but its not working. loop's inside math processing not working
Here is output.
sh -c make -s
./main
kac basamakli olsun
3
birinci degeri giriniz3
deger 1:3
1ikinci deger giriniz4
2ucuncu deger giriniz5
34.deger giriniz2
45.deger giriniz1
5birinci degeri giriniz4
deger 1:4
1ikinci deger girinizsignal: terminated
Look at this code
int i;
cout<<"kac basamakli olsun"<<endl;
cin>>basamaksayisi;
while(basamaksayisi!=i)
What do you think the value of i is here?
No idea about MatLab but in C++ the variable i has not been given a value before it is used, which automatically means that this code is bugged.

C++ - Reading from a file bug

My program is working as I want it to work (It is on Bosnian language so the cout of the program is not that important). The program is about reading some text from file and then couting what I want to
The only problem I have with this program is that for some reason it is showing the two 0 0 at the end of the text file even though I do not have it in my text file
Here is the code:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
struct proizvod
{
char naziv[100];
char proizvodac[100];
int cijena = 0;
int kolicina = 0;
};
bool poredjenje(proizvod a, proizvod b)
{
if (a.cijena != b.cijena )
return a.cijena > b.cijena;
}
void sortiranje(proizvod a[], int n)
{
sort(a, a+n, poredjenje);
}
int main()
{
ifstream datoteka;
datoteka.open("proizvodi.txt.txt");
int brojStvari = 0;
int sumaProizvoda = 0;
int ukupnaVrijednost = 0;
char* spisakProizvoda[100];
int brojFIAT = 0;
int spisakCijena = 0;
proizvod automobili[100];
if (datoteka.fail())
{
cout << "Ne postojeca datoteka";
exit(1);
}
while (datoteka.good() && !datoteka.eof())
{
datoteka >> automobili[brojStvari].naziv >> automobili[brojStvari].proizvodac >> automobili[brojStvari].cijena >> automobili[brojStvari].kolicina;
++brojStvari;
++sumaProizvoda;
}
for (int i = 0; i < brojStvari; i++)
{
cout << automobili[i].naziv << " " << automobili[i].proizvodac << " " << automobili[i].cijena << " " << automobili[i].kolicina << endl;
}
for (int i = 0; i < brojStvari; i++)
{
ukupnaVrijednost += automobili[i].cijena;
if (automobili[i].kolicina == 0)
{
spisakProizvoda[i] = automobili[i].proizvodac;
}
else if (automobili[i].proizvodac == "FIAT")
{
brojFIAT++;
}
}
char pomocna[100];
cout << endl;
cout << "Ukupan broj proizvoda u datoteci: " << sumaProizvoda << endl;
cout << "Ukupan vrijednost proizvoda u datoteci: " << ukupnaVrijednost << endl;
cout << "Spisak automobila sa cijenom 0 su: ";
for (int i = 0; i < brojStvari; i++)
{
if (!spisakProizvoda[i])
{
cout << "Ne postoje ti proizvodi " << endl;
break;
}
else
cout << spisakProizvoda[i] << endl;
}
cout << "Broj prozivoda koji proizvodi FIAT: " << brojFIAT << endl;
cout << "Sortirani proizvodi prema cijeni: " << endl;
sortiranje(automobili, brojStvari);
for (int i = 0; i < brojStvari; i++)
{
cout << automobili[i].proizvodac << endl;
}
return 0;
}
And here is the cout
Golf Volskwagen 5000 5
AudiRS5 Audi 50000 3
0 0
Ukupan broj proizvoda u datoteci: 3
Ukupan vrijednost proizvoda u datoteci: 55000
Spisak automobila sa cijenom 0 su: Ne postoje ti proizvodi
Broj prozivoda koji proizvodi FIAT: 0
Sortirani proizvodi prema cijeni:
Audi
Volskwagen
Can anybody tell me what is the problem ?
P.S : Sorry if you do not understand the program itself I apologize sincerely
You are seeing the extra 0 0 at end most likely due to an extra empty line at end of proizvodi.txt.txt file. This happens because the new line is also accepted as input but since there are no entries, the entries in struct proizvod retain default 0 values for cijena and kolicina which gets printed out as 0 0.
Update your main() code to reading file like follow and it will work as expected:
while (datoteka >> automobili[brojStvari].naziv >>
automobili[brojStvari].proizvodac >> automobili[brojStvari].cijena >>
automobili[brojStvari].kolicina) {
++brojStvari;
++sumaProizvoda;
}

C++ dice rolling and graph [closed]

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 4 years ago.
Improve this question
#include <iostream>
#include<iomanip>
#include <array>
#include<string>
using namespace std;
int main()
{
const int arraysize = 13;
string n;
int counter[13];
double sum=0;
// init counter
for (int i = 0; i < 13; i++)
counter[i] = 0;
int die1;
int die2;
for (int roll1 = 0; roll1 <= 36000000; roll1++) {
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
counter[die1 + die2]++;
}
cout << "Rolls" << setw(13) << "Frequency" << endl;
for (int face = 2; face < arraysize; face++)
{
for (int s = (counter[face] * 100 / 36000000); s > 0; s--) {
cout << '*'; //output for graph
}
sum = ((sum + counter[face]) / 36000000) * 100;
cout << setw(7) << face << setw(13) << counter[face] << setw(15) << fixed << setprecision(1) << sum << endl;
}
system("Pause");
return 0;
}
output of code
I'm trying to get the output like this but my graph isn't coming out the way I'd like. any suggestions one how i can do it? how do I position it correctly.
and this my output of my code.
so i ended up figuring it out. i ended up placing the graph in a different part of the code and got it working.
for (int face = 2; face < arraysize; face++)
{
sum = ((sum + counter[face]) / 36000000) * 100;
cout << setw(7) << face << setw(13) << counter[face] << setw(15) << fixed << setprecision(1) << sum;
for (int s = (counter[face] * 100 / 36000000); s > 0; s--) {
cout << '*';
}
cout << endl;
}
system("Pause");
return 0;
}
Thanks for the suggestion!

Anyway to make this sorting algorithm more efficient/shorter C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
// Number Sorting Algorithm - Trey Taylor 2014
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
int numbersort [] = {200, 85, 63, 4}
cout << "The numbers scrambled are: ";
cout << numbersort[0] << ", ";
cout << numbersort[1] << ", ";
cout << numbersort[2] << ", ";
cout << numbersort[3] << ", " << endl;
firstlast:
if (numbersort[0] > numbersort[1]) {
int temp = 0;
temp = numbersort[0];
numbersort[0] = numbersort[1];
numbersort[1] = temp;
}
if (numbersort[1] > numbersort[2]) {
int temp = 0;
temp = numbersort[1];
numbersort[1] = numbersort[2];
numbersort[2] = temp;
}
if (numbersort[2] > numbersort[3]) {
int temp = 0;
temp = numbersort[2];
numbersort[2] = numbersort [3];
numbersort[3] = temp;
}
while (numbersort[0] > numbersort[1]) {
goto firstlast;
}
cout << "The numbers unscrambled are: ";
cout << numbersort[0] << ", ";
cout << numbersort[1] << ", ";
cout << numbersort[2] << ", ";
cout << numbersort[3] << ", ";
}
Does anybody know if there is a way to use a for or while loop to rearrange the numbers in the array into ascending order from left to right rather than just using 3 if statements
You could use std::sort or bubble sort algorithm(link how it works). Algorithm checks is current number in loop bigger from next one. If it is change those numbers in array. At the end you get the biggest number on the end of array and smallest at beggining of array.
int main()
{
int numbersort[] = { 200, 85, 63, 4 };
int temp = 0;
// This loop sorts all numbers in the array
for (int z = 0; z < 4; ++z)
{
// This loop sorts only one number to the end
for (int i = 0; i < 3; ++i)
{
if (numbersort[i] > numbersort[i + 1])
{
temp = numbersort[i];
numbersort[i] = numbersort[i + 1];
numbersort[i + 1] = temp;
}
}
}
cout << "Sorted numbers" << endl;
for (int i = 0; i < 4; ++i)
{
cout << numbersort[i] << endl;
}
system("pause");
return 0;
}
Something like this:
i = 0;
while (i < (sizeof(numbersort)/sizeof(numbersort[0])) - 1)
{
if (numbersort[i] >= numbersort[i + 1])
{
std::swap(numbersort[i], numbersort[i + 1]);
i = 0;
}
else
{
++i;
}
}

returning index of the biggest value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
This is the program that I need to solve:
Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people (Person 1, Person 2, ..., Person 10).
Once the data has been entered the program must analyze the data and output which person ate the most pancakes for breakfast.
Could anybody write the code for returning the index of biggest value (I have labeled it as "h")
#include "iostream"
using namespace std;
int main()
{
int x[11];
int y;
int h;
for (int i = 1; i <= 10; i++)
{
cin >> i[x];
cout << "Person: " << i << " has eaten " << i[x] << " pancakes" << endl
y = x[0];
h = x[0];
for (int j = 1; j <= 10; j++)
{
if (x[j] > y)
{
y = x[j];
}
}
}
cout << "The most pancakes are eaten by Person " << h << " with " << y << endl;
system("pause");
return 0;
}
Not tested, should work:
#include <iostream>
using namespace std;
int main()
{
int x[11];
int ans, ansmax = 0;
for (int i = 1; i <= 10; i++)
{
cin >> x[i]; // You had wrong variable here
cout << "Person: " << i << " has eaten " << x[i] << " pancakes" << endl
if(x[i] > ansmax)
{
ansmax = x[i];
ans = i;
}
}
cout << "The most pancakes are eaten by Person " << ans << " with " << ansmax << endl;
system("pause");
return 0;
}
My five cents
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
const int N = 10;
int person[N];
// Entering initial data
for ( int i = 0; i < N; i++ )
{
cout << "Enter number of pancakes eaten by person " << i + 1 << ": ";
cin >> person[i];
}
// Finding the index (favorite) of the person who has eaten the most pancakes.
int favorite = 0;
for ( int i = 1; i < N; i++ )
{
if ( person[favorite] < person[i] ) favorite = i;
}
// Now all is ready to show the result
cout << "\nThe most pancakes are eaten by Person " << favorite + 1
<< " with " << person[favorite] << endl;
system( "pause" );
return 0;
}
#include <algorithm>
int index = std::max_element(x, x + 11) - x;