I'm a beginner at coding and I can't fix this code and I'm going crazy. It keeps telling me certain variables were not declared and i'm not sure how to fix it.
#include <iostream>
using namespace std;
int main()
{
int (a = 0), sum;{
cout << "This program should read all integer numbers ";
cout << "to sum until a negative number is entered\n";
exit(0);
}
while(a < 0) {
cout << "Enter an integer number: ";
cin >> a;
sum += a;
}
cout << "The sum is sum\n";
return 0;
}
You did not initialize sum, so it could start with any value.
You have extra layers of pointless { } around for no reason.
Your final cout statement does not actually print the varaible.
Change it to: cout << "The sum is " << sum << "\n";
#include <iostream>
using namespace std;
int main()
{
int a = 0, sum = 0;
cout << "This program should read all integer numbers ";
cout << "to sum until a negative number is entered\n";
do {
cout << "Enter an integer number: ";
cin >> a;
if (a > 0)
{
sum += a;
cout << "The sum is currently: " << sum << "; but this is not yet the final value.\n";
}
} while(a > 0) ;
cout << "The sum is " << sum << "\n";
return 0;
}
Lots of gotchas with your code.
Issues I found and corrected:
You have extra parentheses and curly brackets.
You didn't initialize the variable called sum
You initialize the variable called a to 0 but your loop will only execute while a is less than 0
I had to #include "stdafx.h" above where you included iostream
The line that says "The sum is sum" - the second sum should be outside the double quotes so that it will be treated as a variable rather than text.
This code works, please compare it to yours:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int a = 0, sum = 0;
cout << "This program should read all integer numbers ";
cout << "to sum until a negative number is entered\n";
while (a <= 0) {
cout << "Enter an integer number: ";
cin >> a;
sum += a;
}
cout << "The sum is " << sum << "\n";
return 0;
}
Related
C++ code where a user enters 2 integers, then the program outputs how many numbers were multiples of 3 between those integers, including both numbers, and how many numbers were divisible by 5.
Here is my code. I think I am not calling the if statements correctly. Maybe I need a switch?
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int numb1, numb2;
int sentinel;
int counter = 0;
int mult3 = 0;
int mult5 = 0;
cout << "Enter an integer:";
cin >> numb1;
cout << "Enter another integer:";
cin >> numb2;
cout << endl;
sentinel = (abs(numb2-numb1)+1);
if(numb1 % 3 == 0 && counter <= sentinel) {
mult3++;
numb1++;
counter++;
}
else {
numb1++;
counter++;
}
cout << endl;
counter = 0;
if(numb1 % 5 == 0 && counter <= sentinel) {
mult5++;
numb1++;
}
else {
numb1++;
counter++;
}
cout << endl;
cout << mult3 << " " << "numbers are divisible by 3 in between your entered integers." << endl;
cout << mult5 << " " << "numbers are divisible by 5 in between your entered integers.";
cout << endl;
return 0;
}
A short version for your code is given below. Checkout the while loop(not ran, just from top of my head).
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int numb1, numb2;
int sentinel;
int counter = 0;
int mult3 = 0;
int mult5 = 0;
cout << "Enter an integer:";
cin >> numb1;
cout << "Enter another integer:";
cin >> numb2;
cout << endl;
while(numb1 <= numb2){
mult3 += (numb1%3)==0?1:0; // (numb1%3)?0:1
mult5 += (numb1%5)==0?1:0;
++counter;
++numb1;
}
cout << endl;
cout << mult3 << " " << "numbers are divisible by 3 in between your entered integers." << endl;
cout << mult5 << " " << "numbers are divisible by 5 in between your entered integers.";
cout << endl;
return 0;
}
You're not looping it at all, so this will only execute once.
while(counter < sentinel)
{
//run your tests and increment the appropriate variables if the numbers are divisible by 3 or 5
counter++;
}
start by evaluating the lowest of the 2 entered numbers + counter % 3 == 0
I got it. I had to use 3 separate while loops. One for <, one for >, and one for =. When I just had a <= and >=, the first loop would feed into the second. I took out the sentinel and the counter thanks to Avezan's help. Thanks everyone.
I have written a factorial number finder, unforutunately it returns the number that is inputted. Any help is appreciated.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int factorialNumber;
int factorialNumber2;
int counter = 1;
cout << "Enter a number and I shall find its factorial value.";
cin >> factorialNumber2;
factorialNumber=factorialNumber2;
while(counter == factorialNumber2);
{
factorialNumber=counter * factorialNumber;
counter++;
}
cout << factorialNumber;
}
There's some problems here. First, you reset the value of factorialNumber2 to something undefined.
Next problem is that you have a semicolon after the while loop and that the condition for the loop was wrong. Here is working code:
int main()
{
int number;
int factorialNumber;
int counter = 1;
cout << "Enter a number and I shall find its factorial value.";
cin >> number;
factorialNumber=1;
while(counter <= number)
{
factorialNumber=counter * factorialNumber;
counter++;
}
cout << factorialNumber;
}
I renamed the variables, cause factorialNumber and factorialNumber2 didn't really make sense. Another thing you should think of is to not use using namespace. It will hardly affect you at your level, but it's generally a bad idea. Read why here
Also, a pretty good way to start debugging this would have been just some printouts. Here is an example:
int main()
{
int number;
int factorialNumber;
int counter = 1;
cout << "Enter a number and I shall find its factorial value.";
cin >> number;
factorialNumber=1;
cout << "Before loop" << endl
<< "number: " << number
<< " factorialNumber: " << factorialNumber
<< " counter: " << counter << endl;
while(counter <= number)
{
cout << "Begin loop" << endl
<< "number: " << number
<< " factorialNumber: " << factorialNumber
<< " counter: " << counter << endl;
factorialNumber=counter * factorialNumber;
counter++;
cout << "End loop" << endl
<< "number: " << number
<< " factorialNumber: " << factorialNumber
<< " counter: " << counter << endl;
}
cout << "After loop" << endl
<< "number: " << number
<< " factorialNumber: " << factorialNumber
<< " counter: " << counter << endl;
cout << factorialNumber;
}
It looks a bit crappy, but those printouts are intended to be temporary anyway. Also, of course this is a bit overkill, but it shows the general idea.
Here is what it would have looked like with your original code:
$ ./a.out
Enter a number and I shall find its factorial value.5
Before loop
number: -155928352 factorialNumber: -155928352 counter: 1
Begin loop
number: -155928352 factorialNumber: -155928352 counter: 1
End loop
number: -155928352 factorialNumber: -155928352 counter: 2
After loop
number: -155928352 factorialNumber: -155928352 counter: 2
-155928352
With a little experience, you can clearly see what's wrong here. Lastly, use compiler flags to get warnings. Here is an example:
$ g++ yourcode.cpp -Wall -Wextra
yourcode.cpp: In function ‘int main()’:
yourcode.cpp:18:3: warning: this ‘while’ clause does not guard... [-Wmisleading-indentation]
while(counter == factorialNumber2);
^~~~~
yourcode.cpp:19:3: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘while’
{
^
In your code remove semicolon after while loop condition statement
and replace
factorialNumber=factorialNumber2;
with
factorialNumber=1;
Also chnage while loop condition to
while(counter <= factorialNumber2)
Because this is the variable you're using to store factorials. SO firstly value should be 1.
Whole Code will become
#include <iostream>
#include <string>
using namespace std;
int main()
{
int factorialNumber;
int factorialNumber2;
int counter = 1;
factorialNumber2=5;
factorialNumber=1;
while(counter <= factorialNumber2)
{
factorialNumber=counter * factorialNumber;
counter++;
}
cout <<"Value is"<< factorialNumber;
}
The Most efficient way of find out factorial is using recursive function.
#include <iostream>
#include <string>
using namespace std;
float fact;
int main()
{
int inputNumber;
cout << "Enter a number and I shall find its factorial value.";
cin >> inputNumber;
float result=factorial(inoutNumber);
cout << result;
}
float factorial(n) {
fact = 1;
for i = 1 to N do
fact = fact*i;
return fact;
}
Your while condition is counter==factorialNumber2" and its mean that it will run only when both variable values are equal. So while condition always false because couneter start from 1. That's why your program return the value you entered . so try to use following code.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int factorialNumber;
int factorialNumber2;
int counter = 1;
cout << "Enter a number and I shall find its factorial value.";
cin >> factorialNumber2;
factorialNumber= factorialNumber2;
while(counter <= factorialNumber2);
{
factorialNumber=counter * factorialNumber;
counter++;
}
cout << factorialNumber;
}
Hello everyone I'm trying to find the average of a random amount of numbers that are input into a loop. For sum reason after the loop im able to print the right total, but when i try to find the average i get a weird answer. can anyone help me with this or direct to a thread on here that could help? I wasnt able to find anything on here.
here is my code for the program that isnt working.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inData;
string golferFile;
string golfer;
int matches;
int score;
cout << endl;
cout << "Enter the golfer's filename: ";
cin >> golferFile;
inData.open(golferFile.c_str());
getline(inData, golfer);
inData >> matches;
cout << endl;
cout << "The golfer " << golfer << " has " << matches << " matches with scores"
" of" << endl;
cout << endl;
int count;
count = 1;
int matchNumber;
matchNumber = 1;
int sum;
while(count <= matches)
{
inData >> score;
cout << "Match " << matchNumber << ": " << score << endl;
matchNumber++;
count++;
sum = sum + score;
}
}
int mean;
mean = sum / matches;
cout << "The mean score is " << mean << endl;
return 0;
}
the output i receive is this for the mean
The mean score is 1399255
I found several error in your code.
you forget to initialize your sum variable.
in while loop an extra brace found.remove it
you didn't write anything to stop your loop.that why your loop run infinite time.so initialize you loop also.
I have this code so far that is supposed to keep asking the user for a number until they type 0. Then the program will tell the user how many odds and evens they typed. I cannot get the latter function to work correctly. Any tips? I am a beginner, so please no advanced ways to solve this :D
#include <iostream>
using namespace std;
int main ()
{
int n;
int myCounter1, myCounter2;
cout << "Odds and Evens\n\n" << endl;
do
{
cout << "Please enter an integer: ";
cin >> n;
myCounter1 = 0;
myCounter2 = 0;
if (n%2 == 0)
{
myCounter1++;
}
else
{
myCounter2++;
}
}
while (n!=0);
cout << "You entered " << myCounter1 << " even numbers, and " << myCounter2 << "odd numbers " << endl;
return 0;
}
A couple things:
Code indentation (or lack thereof) makes this really hard to read. Indentation is not only cosmetic, but can help in understanding code.
You are setting the counter variables to zero each time the loop runs. Declare them outside of the loop so they retain their values.
The else clause of the if statement has erroneous syntax. Use a simple else instead, as there are only two cases for the parity of n.
When the user types 0 to exit the loop, it too is counted as an even integer. Add a condition in the if statement to account for this.
Applying these changes yields this code:
int n;
int myCounter1 = 0, myCounter2 = 0;
cout << "Odds and Evens\n\n" << endl;
do {
cout << "Please enter an integer: ";
cin >> n;
if (n%2 == 0 && n != 0)
{
myCounter1++;
}
else
{
myCounter2++;
}
} while (n!=0);
cout << "You entered " << myCounter1 << " even numbers, and " << myCounter2 << "odd numbers " << endl;
This
else n == 0
{
myCounter2++;
}
should be
else
{
myCounter2++;
}
Honestly, I don't even know why it didn't grab your attention, since it can't compile.
Also, you shouldn't set the counters to zero in the loop. So
int myCounter1, myCounter2;
cout << "Odds and Evens\n\n" << endl;
do
{
cout << "Please enter an integer: ";
cin >> n;
myCounter1 = 0;
myCounter2 = 0;
should be
int myCounter1=0, myCounter2=0;
cout << "Odds and Evens\n\n" << endl;
do
{
cout << "Please enter an integer: ";
cin >> n;
And, finally, since you probably shouldn't count the 0 as one of the integers entered...
cout << "You entered " << myCounter1-1 << " even numbers, and " << myCounter2 << " odd numbers " << endl;
You have 2 bugs and 1 syntax error.
line:else n == 0 should be simply else
The 2 bugs are related to your counters:
1) You have to exclude the 0 input from the counters.
2) Every time you are reading a number your are setting them (the counters) to zero, which means that you will always ending with zero and one.
Here it is for anyone interested:
include
using namespace std;
int main ()
{
int n;
int myCounter1 = 0;
int myCounter2 = 0;
cout << "Odds and Evens\n\n" << endl;
do
{
cout << "Please enter an integer: ";
cin >> n;
if (n%2 == 0)
{
myCounter1++;
}
else
{
myCounter2++;
}
}
while (n!=0);
cout << "You entered " << myCounter2 << " odd numbers, and " << myCounter1-1 << " even numbers " << endl;
return 0;
}
I am currently learning to code c++ by using the web page program, where I am doing a course. Now recently I got the following exercise:
Using a while or a do-while loop, make a program that asks the user to enter numbers and keeps adding them together until the user enters the number 0.
I wrote the following code in the hope that it would bring the exercise to conclusion:
#include <iostream>
using namespace std;
int main(void){
int sum = 0;
int number;
do
{
cout <<endl;
cin >> number;
sum += number;
cout << "The total so far is: " << sum << endl;
} while (number != 0);
cout << "The total is: " << sum << endl;
}
Yet when I run the code I get the following feedback from the website (there are two links one on the left and the other on the right):
Instructions of the exercise and
Webpage feedback on the exercise
Can you tell me what am I doing wrong, alternatively can you propose an alternative solution then the code I provided? Thank you for any feedback!
The working code is:
#include <iostream>
using namespace std;
int main(){
int sum = 0, numbers;
do{
cout << "The total so far is: " << sum << endl;
cin >> numbers;
cout<< "Number: "<< numbers;
sum += numbers;
} while (numbers != 0);
cout << "The total is: " << sum << endl;
return 0;
}
You have a mistake in the line cout>>endl;. Also, the output should match the instructions. This is why your answer was "wrong".
I think you should design the exact same output as the instructions.
#include <iostream>
using namespace std;
int main(){
int sum = 0, numbers;
do{
cin >> numbers;
sum += numbers;
cout << "The total so far is: " << sum << endl;
} while (numbers != 0);
cout << "The total is: " << sum << endl;
return 0;
}
As for me then I would write the program the following way
#include <iostream>
int main()
{
int sum = 0;
int number;
std::cout << "Enter a sequence of numbers (0-exit): ";
while ( std::cin >> number && number != 0 ) sum += number;
std::cout << "The total is: " << sum << std::endl;
}
If you need to output partial sums then you can add one more output statement
#include <iostream>
int main()
{
int sum = 0;
int number;
std::cout << "Enter a sequence of numbers (0-exit): ";
while ( std::cin >> number && number != 0 )
{
std::cout << "The total so far is: " << ( sum += number ) << std::endl;
}
std::cout << "\nThe total is: " << sum << std::endl;
}
I am guessing the website is doing a simple comparison check.
Could you remove the first cout << endl;
As that would be closer to the expected output.
As to why you are not getting the "total so far" has me stumped.
Do you see the "total so far" text when ran locally?
You should check whether user inputs a number or a character for making sure the adding operation