hello i want to build a programme that finds even and odd number from given two numbers.
when i build it it says succeed
but still it says
warning C4700: uninitialized local variable 'number' used
when i debug it debug error appeared
how to solve this? and can anybody tell me the reason it happens?
thank you so much
below is the code
#include<stdio.h>
#include <iostream>
using namespace std;
int main(){
int number;
int firstNum,secondNum;
cout << "Enter the first number: ";
cin >> firstNum;
cout << "Enter the second number: ";
cin >> secondNum;
if(number % 2 !=0){
for(number = firstNum;number <= secondNum; number++)
cout << number<< " ";
cout << "Odd numbers in given range are: ";
cout << number<< " ";
}
else if(number % 2 ==0){
for(number = firstNum;number <= secondNum; number++)
printf("\nEven numbers in given range are: ");
cout << number << " ";
}
return 0;
}
The reason your program gives error is because you haven't initialized your variable number. I have corrected that. There were few other bugs too which would have given wrong output.I have corrected and reformatted your code a bit -
#include<stdio.h>
#include <iostream>
using namespace std;
int main(){
int number;
int firstNum,secondNum;
cout << "Enter the first number: ";
cin >> firstNum;
cout << "Enter the second number: ";
cin >> secondNum;
number=firstNum;
for(number = firstNum;number <= secondNum; number++)
cout << number<< " ";
//Following piece will print out all odd numbers
cout << "\nOdd numbers in given range are: ";
for(number = firstNum;number <= secondNum ; number++)
if(number&1)
cout<<number<<" ";
cout<<"\n";
//Following piece will print out all even numbers
cout << "Even numbers in given range are: ";
for(number = firstNum;number <= secondNum ; number++)
if(!(number&1))
cout<<number<<" ";
cout<<"\n";
return 0;
}
You are probably trying to print the odd numbers and even numbers in a given range. The above program does exactly that.
Hope this solves your question !
Related
Note: I am a beginner in C++, so please bear with me if there are any serious programming errors that can be fixed easily.
Edit: Options 3 and 4 work perfectly fine without any errors. However, Option 2 has a serious looping problem where 'Error! Number should be in range of (1 to 100)' and 'Enter the number again:' loop continuously when you input any key. Will change the code to show the code for Option 2 and remove Option 3 and Option 4's code.
I created a math program that can calculate numbers, calculate fractions, among other features I added. I added a continue button on some programs (Option 2) that when you enter 'Y' on your keyboard, it should loop the program until the user types a different key to signify that the program should stop. However, the continue button seems not to work. When I press any other key, the program still loops and I have to stop the program so it cannot loop.
#include <<iostream>>
#include <<cmath>>
using namespace std;
int main()
{
std::cout << "Math Application Version 0.1 (currently in development)\n";
std::cout << "Choose an application to use (use numbers 1 - 10)\n":
std::cout << "Option 1: Calculator" << std::endl "Option 2: Use Average Calculator" <<
std::endl << "Option 3: Use Fraction Calculator\n" << std::endl <<
"Option 4: Use LCM (Lowest Common Denominator) Calculator\n";
int choice;
std::cin >> choice;
switch (choice)
{
case 1:
// this is blank on purpose because this would get too long if I added more features here
case 2:
{
printf("\n Chose average calculator.");
char d;
int n, i;
float num[100],
sum=0.0,
average;
anon:
{
cout << "Enter the numbers of data (limit is 100) : ";
cin >> n;
while (n > 100 || n <= 0)
{
cout << "Error! Number should be in range of (1 to 100)." << endl;
cout << "Enter the number again: ";
cin >> n;
}
for (i = 0; i < n; ++i)
{
cout << i + 1 << ". Enter number: ";
cin >> num[i];
sum += num[i];
}
average = sum / n;
cout << "Average = " << average;
}
cout << "\nDo you want to continue? "; cin >> n;
if (n== 'Y', "Yes")
goto anon;
system("PAUSE");
return 0;
break;
}
I'd appreciate any help on this issue or a detailed explanation since this is very confusing for me.
Your code is fine but you just have some typos in these lines.
cout << "\nDo you want to continue? ";
cin >> n;
/*Here => */ if (n== 'Y', "Yes")
fix it to if(n == 'Y'), also you have unintentionally used n instead of the char d that you have defined to use as a check.
So your code should be
cout << "\nDo you want to continue? ";
cin >> d;
if (d == 'Y') { .... }
And for completion, avoid goto whenever you can. You can use a while loop instead of the assembly-like goto.
This is your code but with a while loop instead of goto
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
std::cout << "Math Application Version 0.1 (currently in development)\n";
std::cout << "Choose an application to use (use numbers 1 - 10)\n";
std::cout << "Option 1: Calculator" << std::endl << "Option 2: Use Average Calculator" <<
std::endl << "Option 3: Use Fraction Calculator\n" << std::endl <<
"Option 4: Use LCM (Lowest Common Denominator) Calculator\n";
int choice;
std::cin >> choice;
switch (choice)
{
case 1:
// this is blank on purpose because this would get too long if I added more features here
case 2:
printf("\n Chose average calculator.");
char d = 'Y';
int n, i;
float num[100],
sum=0.0,
average;
while (d == 'Y'){
cout << "Enter the numbers of data (limit is 100) : ";
cin >> n;
while (n > 100 || n <= 0)
{
cout << "Error! Number should be in range of (1 to 100)." << endl;
cout << "Enter the number again: ";
cin >> n;
}
for (i = 0; i < n; ++i)
{
cout << i + 1 << ". Enter number: ";
cin >> num[i];
sum += num[i];
}
average = sum / n;
cout << "Average = " << average;
cout << "\nDo you want to continue? ";
cin >> d;
}
break;
}
}
Hello im stuck at SUBTRACTION AND DIVITION AND I CANT FIGURE OUT WHAT CODE to use because when I try to subtract 10 i inputed it then it will loop since the while condition is not meet which it needs to be negative to terminate the loop and i inputed 2 for the second number then loop again then i putted -number which lead to terminate loop and subtract all the number but the result is -12 its always wrong in every number cant figure out why Please help
Also with divition, only my addition is working havent started the divition cuz i cant figure out how
#include <iostream>
using namespace std;
int amt2, total;
double subNumbers();
double amt=1;
double number=0;
int main() {
int chc=0;
int amt = 0;
int amt2 = 1;
cout << "Welcome again User!\n";
cout << "______________________________________________________________\n" << endl;
cout << "Mathematical Operations(Improved):\n\n";
cout << "\t[1]-Addition" << endl;
cout << "\t[2]-Subtraction" << endl;
cout << "\t[3]-Multiplication" << endl;
cout << "\t[4]-Division\n" << endl;
cout << "______________________________________________________________\n" << endl;
cout << "Type the number corresponding to your chosen operation: ";
cin >> chc;
```
switch (chc) {
case 1:
```
system ("cls");
cout << "\n\n\tOperation chosen: Addition";
cout << "\n______________________________________________________________" << endl;
cout << "\n\nInput positive numbers to use the operation and input a negative number to end the operation.\n\n";
cout << "Enter your number: ";`
cin >> number;
while (number >= 0) {
// add all positive numbers
amt += number;
// take input again if the number is positive
cout << "Enter another number: ";
cin >> number;
}
// display the sum
cout << "\nThe sum of all the numbers is: " << amt << endl;
break;
```
case 2:
system ("cls");
cout << "\n\n\tOperation chosen: Subtraction";
cout << "\n______________________________________________________________" << endl;
cout << "\n\nInput positive numbers to use the operation and input a negative number to end the operation.\n\n";
do{
cout << "Enter your number: ";
cin >> number;
amt=number-number ;
}while (number >= 0);// subtract all positive numbers
// display the difference
cout << "\nThe difference of all the numbers is: "<<amt;
return 0;
}}
```
enter code here
You are subtracting number from number:
amt = number - number; // Which is always 0
So that's why amt == 0 always.
So just change your loop to this:
while (true) {
cout << "Enter your number: ";
cin >> number;
if (number < 0) break;
if (amt == 0) amt = number;
else if (number >= 0) amt -= number;
}
What this does is that if amt == 0, then set amt to number. I have done this because as the default value of amt is 0 (due to int amt = 0;), when amt == 0, then we can assume that the user has entered the first number, and thus we can set amt to number. And then we can use -= operator, which basically means:
amt = amt - number;
But before all this, using if (number < 0) break; we can check if the user has entered a negative number, and if the user has entered a negative number, then the break keyword will break out of the while loop.
Question is how to get the program to ask for input until a valid choice (4-20) is given by the user? I just need to know how to repeat a question. So if a user inputs the wrong number then it will ask the user to input a valid number until the user enters a correct number between 4-20.
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main() {
int num; //integer for number
int roll; //integer for roll
srand(time(0)); //seed random number generator
cout << "You are about to roll a single die" << endl;
cout << "How many sided die would you like to roll(4-20)? ";
cin >> num;
cin.ignore();
roll = 1 + rand() % num; //mods random number
if (num >= 4 && num <= 20) {
cout << "You rolled: " << endl;
cout << roll << endl;
}
else
{
cout << "Please play again and enter a number between 4 and 20" << endl;
cout << "Press RETURN to continue..."<<endl;
cin.get();
cout<<"Exiting"<<endl;
}
return 0;
}
Try this
while(1)
{
cout << "Please enter a number between 4 and 20: ";
cin >> num;
if (num >= 4 && num <= 20)
{
//mods random number
//do something
break;
}
}
Do a while loop to keep on asking for input and have a if statement to break when it is between 4 and 20.
hello i want to build a programme that display even and odd numbers from given numbers
i get this message error which is error C2061: syntax error : identifier 'cout'
i try again to look but i cant find any.i search online and says that i should using instead of which i did but it didnt work too. How to solve this
below is my code
#include<stdio.h>
#include <iostream>
using namespace std;
int main(){
int number;
int firstNum,secondNum;
int countOdd,countEven;
cout << "Enter the first number: ";
cin >> firstNum;
cout << "Enter the second number: ";
cin >> secondNum;
if (firstNum>secondNum )
cout << "Sorry the first number must be less than second number";
else if
cout << "Odd numbers in given range are: ";
for(number = firstNum;number <= secondNum; number++)
if(number % 2 !=0)
cout << number<< " ";
countOdd = number;
cout << "\nTotal count of odd number is :" << countOdd << endl;
printf("\nEven numbers in given range are: ");
for(number = firstNum;number <= secondNum; number++)
if(number % 2 ==0)
cout << number << " ";
countEven = number;
cout << "\nTotal count of even number is :" << countEven << endl;
return 0;
}
You have problem with your else statement. here is how you should write it:
else {
cout << "Odd numbers in given range are: ";
for(number = firstNum;number <= secondNum; number++)
if(number % 2 !=0){
cout << number<< " ";
countOdd++;
}
cout << "\nTotal count of odd number is :" << countOdd << endl;
printf("\nEven numbers in given range are: ");
for(number = firstNum;number <= secondNum; number++)
if(number % 2 ==0){
cout << number << " ";
countEven++;
}
cout << "\nTotal count of even number is :" << countEven << endl;
}
also, don't forget to initialize countOdd and countEven to zero at the beginning of your program.
I have a program that asks a user how many quizzes they would like to input, stores the grades, and the computes the average. The grade has to be between 0 and 100. However, when I enter a grade above 100 the average is wrong. It does not reset the sum. If I enter 120 and it prompts me to reenter the grades, it still uses 120 when calculating the average. How do I get it to reset?
Note: I do not want to change the the format (such as using a while loop).
#include <iostream>
using namespace std;
int main(){
int n, i;
float num[13], sum=0.0, average;
cout << "Enter the numbers of quizzes from 1-13: ";
cin >> n;
while (n < 1 || n > 13)
{
cout << "You must enter a number from 1 - 13." << endl;
cout << "Enter the number of quizzes you would like to input(1-13): “;
cin >> n;
}
for(i=0; i<n; ++i)
{
cout << i+1 << ". Enter number: ";
cin >> num[i];
sum+=num[i];
while (num[i] < 0 || num[i] > 100)
{
cout << “You must enter a number between 0 and 100. Re enter scores.”;
cout << i+1 << ". Enter number: ";
cin >> num[i];
}
}
average=sum/n;
cout << "Average = " << average;
return 0;
}
Just put the sum+=num[i] behind the while (num[i] < 0 || num[i] > 100) ... loop !
This way you only sum up valid numbers.
for(i=0; i<n; ++i)
{
cout << i+1 << ". Enter number: ";
cin >> num[i];
//sum+=num[i]; remove here !!
while (num[i] < 0 || num[i] > 100)
{
cout << “You must enter a number between 0 and 100. Re enter scores.”;
cout << i+1 << ". Enter number: ";
cin >> num[i];
}
sum+=num[i]; // <- insert here
}
for(i=0; i<n; ++i)
{
cout << i+1 << ". Enter number: ";
cin >> num[i];
while (num[i] < 0 || num[i] > 100)
{
cout << “You must enter a number between 0 and 100. Re enter scores.”;
cout << i+1 << ". Enter number: ";
cin >> num[i];
}
}
sum+=num[i];
average=sum/n;
cout << "Average = " << average;
return 0;
}