why inside of loops not processing? [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 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.

Related

ERROR CODE ABOUT STRUCT array, shows 0 on the output [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 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

wrong output in common number array program [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 1 year ago.
Improve this question
I need my code to output the common numbers from two arrays but I can't seem to find the issues in my code. Here are some more words so I can fill the quota for stack's post requirement.
Here's the code.
#include <iostream>
using namespace std;
int main()
{
//Variables
int list1[5], list2[5];
cout << "Enter numbers for List No.1" << endl;
for (int i = 1; i <= 5; i++) {
cout << "Enter number " << i << ":";
cin >> list1[i];
}
cout << "Enter numbers for List No.2" << endl;
for (int i = 1; i <= 5; i++) {
cout << "Enter number " << i << ":";
cin >> list2[i];
}
{
cout << "common elements" << endl;
for (int i = 0; list1; i < i++) {
for (int i = 0; list2; i < i++)
if (list1[i] == list2[i]) {
cout << " " << list1[i];
}
}
}
return 0;
}
You should use different counter for each loop and also your loop has wrong parameters. Do it like:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (list1[i] == list2[j]){
cout<<" "<<list1[i];
}
}
}
Also for initial values start from zero index([0]) as zero index is the first element for each array:
for (int i = 0; i < 5; i++) {
cout << "Enter number " << i << ":";
cin >> list1[i];
}
Another way to do this is by using std::array. It acts very similar to a normal array but it is much better to work with.
for example if you want a char array of length of 10 you can simply write:
std::array<char, 10> My_happy_array.
First please read this to understand why you should not using namespace std. Second consider reading about Range-based for loop
Third, here is the modified version of your program which uses std::array instead of a normal array:
#include <iostream>
#include <array>
int main() {
std::array<int,5> list1;
std::array<int,5> list2;
std::cout << "Enter numbers for List No.1" << std::endl;
for (int i = 0; i < list1.size(); ++i) {
std::cout << "Enter number " << (i+1) << ":";
std::cin >> list1[i];
}
std::cout << "Enter numbers for List No.2" << std::endl;
for (int i = 0; i < list2.size(); ++i) {
std::cout << "Enter number " << (i+1) << ":";
std::cin >> list2[i];
}
for(auto i : list1){
for(auto j : list2){
if(i==j){
std::cout << " " << i;
}
}
}
return 0;
}
Here's fixed code
#include <iostream>
using namespace std;
int main()
{
//Variables
int list1[5] ,list2[5];
cout << "Enter numbers for List No.1" << endl;
for(int i = 0; i < 5; i ++)
{
cout << "Enter number " << i << ":";
cin >> list1[i];
}
cout << "Enter numbers for List No.2" << endl;
for(int i = 0; i < 5; i ++)
{
cout << "Enter number " << i << ":";
cin >> list2[i];
}
cout<<"common elements"<<endl;
for (int i = 0;i<5; i++)
{
for (int j = 0;j<5; j++)
{
if (list1[i] == list2[j])
{
cout<<" "<<list1[i];
}
}
}
return 0;
}
mistakes in code:
you used for loops wrongly whole the time.
array of 5, stores values in index (0,1,2,3,4) not (1,2,3,4,5), so always start loop from 0 and ends before max index (for this case '4' ie. use 'i<5')
while using for loop inside other for loop never use same iterable variable like you used i in both loops use i and j different for each loop,
take a look in my code..
ask me if you get any difficulties.

What should I do if the const value is still 'changed' by inputting a value in C++? [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 1 year ago.
Improve this question
This is a small software for score statistics, which has two main variables: ppl and type, the latter represents the number of subjects to be counted.
However, when this program runs the inner loop for the second time, it will define ppl as the value entered by the user, even though ppl is a const value.
ppl which was 'changed' by inputting a value
When I tried to change the array of grades to a string array, this problem disappeared. Although I can convert data forcedly, I wanna know what is the solution to this problem? Thanks!
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, const char * argv[]) {
// insert code here...
int input = 0;
cout << "How many people do you want to enter grades for?" << endl;
cin >> input;
const int ppl = input;
cout << flush;
cout << "How many subject grades would you like to enter for these people?" << endl;
cin >> input;
const int type = input;
int score[][type] = {};
for (int i = 0; i < ppl; i++) {
for (int j = 0; j < type; j++) {
cout << "Please enter the number " << i + 1 << " person's number " << j + 1 << " score:" << endl;
cin >> score[i][j];
cout << "Successful!" << endl;
// Debug
cout << "ppl: " << ppl << ", type: " << type << endl;
}
}
cout << "Results Statistics" << endl;
for (int l = 0; l < ppl; l++) {
cout << "Number " << l << ": ";
for (int m = 0; m < type; m++) {
cout << score[l][m] << " ";
}
cout << endl;
}
return 0;
}
int score[][type] = {};
is bad. Variable-Length Array is not in the standard C++ and it has less elements than used, so out-of-range access will happen and some data may be broken.
Use std::vector instead of that:
std::vector<std::vector<int> > score(ppl, std::vector<int>(type));

is there a better way i could have written this program? [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 9 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int c;
int d;
int e;
int f;
int aa = 0;
int bb = 0;
int cc = 0;
int dd = 0;
int ee = 0;
int ff = 0;
const string odd = "ODD";
const string even = "EVEN";
cout << "enter 6 numbers " << endl;
cin >> a;
cin >> b;
cin >> c;
cin >> d;
cin >> e;
cin >> f;
aa = a % 2;
bb = b % 2;
cc = c % 2;
dd = d % 2;
ee = e % 2;
ff = f % 2;
if(aa == 0){
cout << even << endl;
}else{
cout << odd << endl;
}
if(bb == 0){
cout << even << endl;
}else{
cout << odd << endl;
}
if(cc == 0){
cout << even << endl;
}else{
cout << odd << endl;
}
if(dd == 0){
cout << even << endl;
}else{
cout << odd << endl;
}
if(ee == 0){
cout << even << endl;
}else{
cout << odd << endl;
}
if(ff == 0){
cout << even << endl;
}else{
cout << odd << endl;
}
return 0;
}
for example is there a way to make it do the same thing but with less code, anything I should have included?
is there an easier way than having to write 6 if/else statements - is there a way to do all 6 in one statement or loop?
how could i improve its efficiency?
Write this function:
void outputEvenness(int n)
{
static const string odd = "ODD";
static const string even = "EVEN";
if(n % 2){
cout << odd<< endl;
} else {
cout << even << endl;
}
}
then call it using outputEvenness(a); outputEvenness(b); etc.
First of all you should include header <string>if you use class std::string.
Also there is no sense to define these strings when they are used as string literals. Also instead of different variables it would be better to define only one array. The auxiliary variables are also unnecessary.
If to assume that you may not use arrays then I would write the program the following way
#include <iostream>
#include <initializer_list>
int main()
{
const size_t N = 6;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
const char *odd = "ODD";
const char *even = "EVEN";
std::cout << "enter " << N << " numbers: ";
std::cin >> a >> b >> c >> d >> e >> f;
for ( int x : { a, b, c, d, e, f } )
{
if ( x % 2 == 0 )
{
std::cout << x << " is " << even << std::endl;
}
else
{
std::cout << x << " is " << odd << std::endl;
}
}
return 0;
}
If you are allowed to use arrays then the program could look as
#include <iostream>
int main()
{
const size_t N = 6;
int a[N] = {};
const char *odd = "ODD";
const char *even = "EVEN";
std::cout << "enter " << N << " numbers: ";
for ( int &x : a ) std::cin >> x;
for ( int x : a )
{
if ( x % 2 == 0 )
{
std::cout << x << " is " << even << std::endl;
}
else
{
std::cout << x << " is " << odd << std::endl;
}
}
return 0;
}
For this simple program there is no sense to define a separate function that will check whether a number is even or odd because it is this program that is such a function.:)
Use arrays and loops:
int a[6]; // Array of 6 ints
cout << "enter 6 numbers" << endl;
// Input the 6 numbers
for (int i = 0; i < 6; i++)
{
cin >> a[i];
}
// Output the results
for (int i = 0; i < 6; i++)
{
cout << a[i] << " is " << (a[i] & 1 ? "ODD" : "EVEN") << endl;
}
int value = 0;
string response = "";
cout << "enter 6 numbers " << endl;
for(int i=0; i<6; i++)
{
cin >> value;
value % 2 == 0 ? response+="even\n" : response+="odd\n";
}
cout << response;

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;