How to get rid of error c2061;syntax error cout? - c++

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.

Related

Printing on same line and add count in one line one time in c++

how to show output ( Number of grades above the Average + Grades above or equal the Average ) in same line i don't wont new line for each number + how to print these one time not many times , i mean because i use for loop it print Number of grades above the Average every number in one line i want just the count in one line how to do this
#include <iostream>
using namespace std;
int main()
{
int G,N;
float num[100], sum=0.0, average;
cout << "Enter the numbers of Student : ";
cin >> N;
while (N > 500 || N <= 1)
{
cout << "Error ! number of student should in range of (1 to 500)." << endl;
cout << "Enter the number again: ";
cin >> N;
}
for(G = 0; G < N; ++G)
{
cout << G + 1 << ". Enter " << G + 1 << " Mark : ";
cin >> num[G];
sum += num[G];
}
// find average
average = sum / N;
cout <<endl<< "Grades Average = " << average <<endl<<endl;
// find Grades above or equal the Average
cout<<"Grades above or equal the Average : "<< endl;
for (G = 0; G < N; ++G){
if (num[G] >= average){
cout << num[G] << endl;
}
}
// find Number of grades above the Average
cout<<endl;
for (G = 0; G < N; ++G){
if (num[G] >= average){
cout << "Number of grades above the Average : " << G + 1 << endl;
}
}
return 0;
}
There are a number of issues with your code:
num[] can hold 100 values max, but you are allowing the user to enter up to 500 values into it, thus you have the potential for a buffer overflow.
you say the valid number of students is 1 to 500, but you are preventing the user from entering 1.
lack of adequate error handling when reading the user's input.
you are outputting "Number of grades above the Average : " inside the loop for every matching grade, which you say you don't want. You should instead output it once before entering the loop, then use the loop to count the matching grades without outputting each one, then output the final count after the loop is finished.
Try this:
#include <iostream>
#include <limits>
using namespace std;
const int maxStudents = 500;
int main()
{
int G, N, nAverages = 0;
float num[maxStudents], sum = 0.0, average;
cout << "Enter the number of Students : ";
do
{
if (cin >> N)
{
if (N >= 1 && N <= maxStudents)
break;
cout << "Error ! Number of students should be in range of (1 to " << maxStudents << ")." << endl;
}
else
{
cout << "Error ! Invalid input." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Enter the number again: ";
}
while (true);
for(G = 0; G < N; ++G)
{
cout << G + 1 << ". Enter Mark : ";
while (!(cin >> num[G]))
{
cout << "Error ! Invalid input." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Enter the mark again: ";
}
sum += num[G];
}
// find average
average = sum / N;
cout << endl << "Grades Average = " << average << endl << endl;
// find Grades above or equal the Average
cout << "Grades above or equal the Average : " << endl;
for (G = 0; G < N; ++G){
if (num[G] >= average){
cout << G + 1 << ": " << num[G] << endl;
++nAverages;
}
}
// Number of grades above the Average
cout << endl << "Number of grades above the Average : " << nAverages << endl;
return 0;
}

how to count the number of "numbers in for"

everyone, I build a programme to know even and odd number from given numbers.
The even and odd number works just fine, and i want to know the total count of numbers ranging from the start number to the end number but it always says 11 at the output for both.
How do I solve this? and is there any function to count letters/numbers because it will be very helpful.
I did search it up but I can't find any Thank you
#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
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;
}
Code to count odd and even numbers:
using namespace std;
// Return the number of odd numbers
// in the range [L, R]
int countOdd(int L, int R){
int N = (R - L) / 2;
// if either R or L is odd
if (R % 2 != 0 || L % 2 != 0)
N += 1;
return N;
}
// Driver code
int main()
{
int L = 3, R = 7;
int odds = countOdd(L, R);
int evens = (R - L + 1) - odds;
cout << "Count of odd numbers is " << odds << endl;
cout << "Count of even numbers is " << evens << endl;
return 0;
}
You are overwriting your counts with every iteration of the for loop. countOdd = number; should be moved inside the if statement and read more like this:
if (number % 2 != 0) {
cout << number << " ";
countOdd++;
}
That way you will actually be counting the numbers as you find them.
This goes the same for the code in the counting of even numbers.
if (number % 2 == 0) {
cout << number << " ";
countEven++;
}
And don't forget to initialize the counters variables before using them:
countOdd = countEven = 0;

How to initialize the variable

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 !

Why does one of my 'while' statements execute when another one of my 'while' statements is true?

Note: I am using C++
Why does one of my 'while' statements ('while' number 1) execute anyway when another one of my 'while' statements ('while' number 2) placed above it is valid? This bothers me, because though 'while' number 1 is not true, but 'while' number 2 is true, 'while' number 1 executes anyway instead of 'while' number 2. Can anyone help me, or explain this? Here is my code:
#include <iostream>
#include <string>
using namespace std;
void PancakeGlutton()
{
int answer;
cout << "Good morning!" << endl;
cout << "Would you like to enter pancake data? Press 1 to accept, press 2 to decline: ";
cin >> answer;
while (answer == 1) {
int totalPeople = 10;
int totalPancakes = 0;
int input;
int lowest = 100000;
int highest = 0;
for (int i = 9; i >= 0; --i) {
cout << "How many pancakes did you eat this morning? I will be asking this question " << i << " more times." << endl;
cin >> input;
totalPancakes += input;
if (input >= highest) {
highest = input;
}
if (input <= lowest) {
lowest = input;
}
}
double pancakeAverage = double(totalPancakes) / double(totalPeople);
cout << "The total number of pancakes eaten was " << totalPancakes << " pancakes " << endl;
cout << "The average number of pancakes eaten was " << pancakeAverage << " pancakes " << endl;
cout << "The highest number of pancakes eaten was " << highest << " pancakes" << endl;
cout << "The lowest number of pancakes eaten was " << lowest << " pancakes" << endl;
cout << "" << endl;
cout << "Do you want to enter more pancake data? Press 1 to accept, press 2 to decline: ";
cin >> answer;
}
// while number 1:
while (answer == 2) {
break;
}
// while number 2:
while (answer != 1 || answer != 2) {
cout << "Error: please enter a valid answer. 1 or 2? ";
cin >> answer;
}
}
int main()
{
PancakeGlutton();
system("PAUSE");
return EXIT_SUCCESS;
}
while (answer != 1 || answer != 2) {
cout << "Error: please enter a valid answer. 1 or 2? ";
cin >> answer;
}
must be
while (answer != 1 && answer != 2) {
cout << "Error: please enter a valid answer. 1 or 2? ";
cin >> answer;
}

Maybe a syntax issue here?

Homework problem I am helping one of my mentees out with (check my history, I have previously asked help with Java in more advanced programs. This is something simple I can't help her figure out). We need to use a while loop to read in numbers, keep track of the count, and keep summing up the numbers entered. We keep getting an error in line 24. Even when I comment it out and run it, the programs doesn't do what it is supposed to do. Been forever since I've done a program in C++ and I need the help of you guys!
#include <iostream>
using namespace std;
int main()
{
int num;
int sum = 0;
int count = 0;
float avg;
cout << "Enter numbers, 999 to quit" << endl;
cin >> num; //
while (num != 999)
{
cout << "Number entered is" << num << endl;
cout << "Enter numbers, 999 to quit" << endl;
cin >> num;
sum = sum + num;
count++;
}
cout << "Total numbers entered: " + count << endl;
cout << "Sum of numbers entered is " + sum << endl;
avg = sum/count;
cout << "Average of numbers entered:" + avg << endl;
return 0;
}
cout << "Total numbers entered: " + count << endl;
cout << "Sum of numbers entered is " + sum << endl;
avg = sum/count;
cout << "Average of numbers entered:" + avg << endl;
Change those +'s to <<'s.
cout << "Total numbers entered: " << count << endl;
cout << "Sum of numbers entered is " << sum << endl;
avg = sum/count;
cout << "Average of numbers entered:" << avg << endl;
#include<iostream>
using namespace std;
int main()
{
int num,count;
float sum,average;
cout << "Enter numbers, 999 to quit" << endl;
cin>>num;
count=0;
sum=0;
while (num!=999)
{
cout<<"Number entered is"<<num<<endl;
++count;
sum+=num;
cout << "Enter numbers, 999 to quit" << endl;
cin>>num;
}
if (count==0) {
count=1;
}// if the first number you enter is 999 count should be 1
// , otherwise avg will be (sum/0 ),which doesn't make sense.
cout << "Total numbers entered: " <<count << endl;
cout << "Sum of numbers entered is " <<sum << endl;
average = sum/count;
cout << "Average of numbers entered:"<<average << endl;
// use << not + because "Total..." is string type and count is int type
system("pause");
return 0;
}
You should pay attention to the type of variable when you do something,which often can cause small errors.