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 8 years ago.
Improve this question
According to the program below, if donuts = 1 it will assign 0 to donuts, but if I run this it shows that 0 is assigned to donuts. Please tell me what I am over looking. Because how I see this code donuts should equal 12, because donuts should have fallen into the else:
#include <iostream> // Header file to enable console I/O
#include <string> // Header file to enable string
#include <iomanip> // enables maniplution of io strem
using namespace std;
// Begin main Function Definition
int main()
{
int donuts = 10;
if (donuts = 1)
{
donuts = 0;
}
else
{
donuts += 2;
}
cout<< donuts;
system("PAUSE");
return 0;
}
// End of main function
if (donuts = 1)
should be:
if (donuts == 1)
//^^^
You need to use logical equal == or assignment =
Related
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 2 years ago.
Improve this question
I have been trying to make a C++ "math tutor" program. It's purpose is to ask the user if they want to do addition, subtraction, or multiplication, and once they make their selection and the calculation is complete, they are prompted to type in 0 to terminate the program and 1 to repeat it.
I tried using a do-while loop for this, but when I ran it, I found that as long as the user pressed 0 or 1, the program would repeat and it would never terminate.
Here's the code (without the math part as that really isn't relevant):
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
/*
* program to serve as math tutor for a young student
*/
int main(int argc, char** argv) {
int repeatChoice = 0;
//actual program has more variables here, but those are all math variables.
do {
//in this area was just code for the actual math part
cout << "Do you want to repeat this program (0 for no, 1 for yes)?" << endl;
cin >> repeatChoice;
while (repeatChoice != 0 && repeatChoice != 1) {
cout << "Invalid input. Please select 0 or 1." << endl;
cin >> repeatChoice;
}
if (repeatChoice = 0)
break;
} while (repeatChoice = 1);
return 0;
}
If anyone thinks seeing the full code will help, I'll edit the question to add it.
Please note that I'm new to programming and haven't researched any complex concepts really, so please if you can, explain it in relatively simple terms.
From what I can see the problem is in this part of the code:
...
if (repeatChoice = 0)
break;
} while (repeatChoice = 1);
...
You are using the assignment operator = instead of the comparison operator ==.
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 8 years ago.
Improve this question
I am about to make a program that will get a record of grades of students and it will determine it's position in a frequency distribution table. The records will be coming from a file. Here is my code:
#inlcude<fstream>
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
int const ns=40;
int main()
{
int y,x,i,vl=0,l=0,m=0,h=0,vh=0;
int argr[ns];
ifstream myfile ( "file.txt", ios::in);
if (myfile.is_open())
{
while(getline(myfile,x))
for(i=0;i<ns;i++)
{
argr[i]=x;
if(argr[i]<=20 && argr[i]>=0)
vl=vl+1;
else if(argr[i]<=40 && argr[i]>=21)
l=l+1;
else if(argr[i]<=60 && argr[i]>=41)
m=m+1;
else if(argr[i]<=80 && argr[i]>=61)
h=h+1;
else if(argr[i]<100 && argr[i]>=81)
vh=vh+1;
cout<<"Range\t\tFrequency\n\n";
cout<<"0-20\t\t "<<vl<<endl;
cout<<"21-40\t\t "<<l<<endl;
cout<<"41-60\t\t "<<m<<endl;
cout<<"61-80\t\t "<<h<<endl;
cout<<"81-100\t\t "<<vh<<endl;
}
myfile.close();
}
else cout<<"Can't find the file";
return 0;
}
Another problem showed saying, "Invalud preprocessing directive#include"
What should I do?
#inlcude<fstream>
change this to
#include<fstream>
you just got a typo but what kind of terrible IDE are you using that does not show you this immediately?
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 8 years ago.
Improve this question
I just learned how to use this operator : ->,
and I am trying to create practice programs so I can remember it and be familiar with it.
I created a program that inputs my health and then heal my (add health) using the -> operator.
But when I run program, my health stays at 50 (cause I set my current health to 50).
here is my code :
#include <iostream>
using namespace std;
struct myhealth
{
unsigned short my_health;
};
void addhealth(myhealth* addhealth)
{
addhealth->my_health += 50;
};
int main()
{
myhealth player;
player.my_health = 50;
cout << "My earlier health : " << player.my_health << endl;
myhealth();
cout << "My current health : " << player.my_health;
cin.get()
return 0;
}
You never call addhealth and so the value is never modified.
This line of code:
myhealth();
appears to have been written in error. Instead I think you meant to write:
addhealth(&player);
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 8 years ago.
Improve this question
I'M trying to make a code that is able to increase int 'd' by one each time the a+b reach 20.
and if there still any number less than 20 then this will be the int 'c'.
but instead of getting the right result in my next program which is
49-0
i get this wrong answer
47-40
what should i do ?
#include <iostream>
using namespace std;
int main(){
int a=50;
int b=18;
int c=a+b;
int d=0;
int i;
for(i=0;i<c;i++)
{
while(c>20)
{
d+=1;
c=c-20;
break;
}}
cout<<d<<"-"<<c;
return 0;
}
The problem is in your while loop:
while(c > 20)
{
d+=1;
c=c-20;
break;
}
The loop will only execute once because of your break statement.
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
i wrote a little algorithm for marge to sorted array. but i have problem with that.
#include <iostream>
using namespace std;
int main() {
// main function started form here:
int firstArray[10] = {1,3,5,7,9,11,13,15,17,19};
int secondtArray[10] = {2,4,6,8,10,12,14,16,18,20};
int mergedArray[20];
int firstCounter=0 , secondtCounter=0 , mergedCounter=0;
while(firstCounter < 10 && secondtCounter < 10){
if(firstArray[firstCounter] < secondtArray[secondtCounter]){
mergedArray[mergedCounter] = firstArray[firstCounter];
firstCounter++;
} else {
mergedArray[mergedCounter] = secondtArray[secondtCounter];
secondtCounter++;
}
mergedCounter++;
}
while(firstCounter < 10) {
mergedArray[mergedCounter] = firstArray[firstCounter];
firstCounter++;
mergedCounter++;
}
while(secondtCounter < 10) {
mergedArray[mergedCounter];
secondtCounter++;
mergedCounter++;
}
for(int j=0; j<20; j++){
//cout << mergedArray[j] << endl;
}
cout << mergedArray[19];
return 0;
}
in outpout for array mergedArray[19] i get something like this: 2686916!!!
i don't know why i get this value. how can i fix that. and why i get this value.
Typo in last while. You may increase your warning level to let your compiler show you your typo (warning: statement has no effect [-Wunused-value]).
while(secondtCounter < 10) {
mergedArray[mergedCounter];
secondtCounter++;
mergedCounter++;
}
should be
while(secondtCounter < 10) {
mergedArray[mergedCounter] = secondtArray[secondtCounter];
secondtCounter++;
mergedCounter++;
}
As pointed out by WhozCraig's comment, you're not assigning any value to mergedArray[19] because you left out the assignment part of the statement.
Since you haven't assigned a value, it's printing out whatever value happens to be at that memory address from previous usage. If you run your program (as it's currently written) several times, you'll see that the number there might change. Also, if you'd printed out the values in mergedArray before assigning anything, you'd see more such meaningless (to you in the current application) numbers.