I'm trying to write in C++ a way to have the user input an double one at a time and have the program output (on entering 0) the number of times those variables differ by at least 1. For example if I enter 1, 1.7, 0.8, -0.1, -1, 0 ... I should get back 4. (or if I just input 0, the output should just be 0). I'm having trouble understanding how to use a while/for operation to make this happen. This is what I have so far.
#include <iostream>
using namespace std;
int main ()
{
double n1, n2;
int n = 0;
cout << "Enter two seperate numbers."
<< endl;
cin >> n1 >> n2;
while (n1 || n2 != 0) {
if (((n2 - n1) > -1)
&& ((n2 - n1) < 1)) {
n++;
n1 = n2;
}
else {
cout << n <<endl;
}
cout << "Enter
another number or enter 0 to quit." << endl;
cin >> n1 >>
endl;
}
return 0;
}
Where have I gone wrong? Am I way off base?
Hi,
From the above problem what I understand is the program is not terminating when the user enters 0 for n1.
It won't stop with the given conditions because, in while loop what you are checking is the while loop should continue when n1 or n2 is not 0. so if n2 has non zero the loop always runs even the n1 is 0. Because the OR condition will give true if any of them are true.
In your case if n2 is non zero and n1 is zero, here while(n!=0 || n2!=0) you used. the n2!=0 condition will always runs.
I have solved and added a bit of code. If you don't need of that case you can remove that part from the program.
I'll make bold what I've changed/modified. in your code.
MyProgram.cpp
#include <iostream>
using namespace std;
int main ()
{
double n1, n2;
int n = 0;
cout << "Enter two seperate numbers." << endl;
cin >> n1 >> n2;
if(n2!=0 && n1!=0){//enter to loop if the n1 and n2
are not zeros.
while (n1!=0) {//we reading only n1 so we wrote n1
only
if (((n2 - n1) > -1) && ((n2 - n1) < 1)) {
n++;
n1 = n2;
}
else {
cout << n <<endl;
}
cout << "Enter another number or enter 0 to quit." <<
endl;
cin >> n1;
}
}else{
cout<<"n2 or n1 is 0";//just a output
}
return 0;
}
I have given the code.
I think there is no need for the output screenshot. If you get any doubt please comment under this answer. I'll clarify your doubt asap.
Thumbs Up!!!
Thank you!!
I'm trying to write in C++ a way to have the user input an double one...
C++ Create a program that finds the dot product of two vectors. I'm currently trying to display the dot product by calling the dotProduct member function however I am confused as to how to do this. What would be the proper way to display the dot product? I don't believe my dotProduct member function is set up correctly to access the proper data. Feel free to modify the dotProduct member function to allow for it to work with the other...
C++ question I'm trying to write a code that takes in n number of variables between 00 and 99. The program should be able to output the 1st and 2nd lowest digits as well as the highest and second highest digits. So if the input is 4 - 50 20 10 70 it should output 10 20 50 70. I think I've gotten the basic code but I can't seem to get the last digit to shift. #include <iostream> #include...
Create a program with the main function and one additional function. The additional function asks for input from the user and returns the output back to the main function for display. This is for c++ and here is my code, could you guys please tell me what I am missing from the instructions. Thank you #include <iostream> using namespace std; int sum (int, int); int main() { int n1, n2, total; cout << "Enter two numbers...
I'm trying to write a program that can ask a user about what type of solid they have and to be able to enter values like radius and length of the shape so the program can calculate the volume of a shape and print the result. I also want to give them a repeating option to input another shape if they want to. I'm trying to use at least 7 functions that can display an output, the volumes of a...
This is what I have as of right now. I am lost on what to do next#include <stdio.h>#include <iostream>using namespace std;int main(){ double scores, numOfStudents, average = 0, highscore, sum = 0; string names; cout << "Hello, please enter the number of students" << endl; cin >> numOfStudents; do { cout << "Please enter the student's name:\n"; cin >> names; cout << "Please enter the student's...
Flow chart of this
program
#include <iostream> #include <cmath> using namespace std; int mainO double sum=0.0, ave-ee, int locmx, locmn int n; cout <<"Enter the number of students: "<<endl; cin >> ni double listln]; double max-0; double min-e; for (int i-e ; i<n ;i++) cout<s enter the gpa: "<cendli cin>>listli]; while (listlile i listli1>4) cout<s error,Try again "<cendl; cin>listlil: sun+=list[i]; N1 if (listli]>max) for(int isin itt) max=list [i]; 10cmx=1+1 ; else if (list [i] min) min=list [i]; locmn-i+1; ave sum/n;...
Hello everyone! I am working on my assignment for C++ and I'm having a bit of trouble on the value producing functions, I am creating a program wherein I have to input a value and I need to create a function called square value where in it will square the value I have input. I cannot seem to figure out this part. Here is the code that I have so far, and thank you for any help. I greatly appreciate...
Hello, The C++ question is: ------------------------------------------------------------------------------- Write a program that takes as input 2 strings from the user. Then it determines if one string is a permutation of the other string. If the program answers "yes" to the previous question, meaning the two strings are permutations of each other, determine if each string has all unique characters. --------------------------------------------------------------------------------- I have completed the first part but I am unsure on how to also determine if each string is all unique characters...
Can you fix this program and run an output for me. I'm using C++ #include using namespace std; //function to calculate number of unique digit in a number and retun it int countUniqueDigit(int input) { int uniqueDigitCount = 0; int storeDigit = 0; int digit = 0; while (input > 0) { digit = 1 << (input % 10); if (!(storeDigit & digit)) { storeDigit |= digit; ...
This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...