hi. i need help please. i just wrote a program that asks the user to type in three integers. now i have to write something that will end the program if the user types in three negative integers. i cant figure out how. my integers are declared using x, y, z. how can i write it. thank you.
Last edited on
If x,y and z, all need to be negative numbers at the same time, you could use
 ``12`` ``````if (x<0 && y<0 && z < 0) // use ending of program here ``````

But, if it's just a negative number, entered 3 different times, then you could use a variable, named something like `int Neg_Num = 0;` the each time you check what the inputs are of x,y and z, add one to Neg_Num. Check if Neg_Num >= 3, and then the cancel program if it is.
how should i end the program? its in a function should i just right return 0; at the end because i used the first option that you gave me and it just continued.
@iky

Use something along these lines, as I show in this small program. Just incorporate this idea into yours. If you're still having problems, please post your code, and I/we, can better help you. And use the <> symbol in box at right to place your program in a code box to make it easier to read.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````// 3 Negative Numbers.cpp : main project file. #include "stdafx.h" // Used with MS Visual Studio. Leave off if using something different #include #include using namespace std; bool numbers(); int main() { bool Neg_Num=false; do { cout << endl << "3 Negative numbers to end program"<< endl << endl << "Enter 3 positve numbers... "; Neg_Num = numbers(); }while(!Neg_Num); cout << endl << endl << "I see you are ending the program. Good-bye!!" << endl << endl; return 0; } bool numbers() { bool negative = false; int x,y,z; int sum; cout << endl << "X = "; cin >> x; cout << "Y = "; cin >> y; cout << "Z = "; cin >> z; if(x<0 && y<0 && z<0) { negative=true; return negative; } sum = x+y+z; cout << "The sum of " << x << ", " << y << " and " << z << " equal " << sum; return negative; }``````

Last edited on
ok so this is what my program looks like. im almost done except for the three negative integers part. thanks.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273`` `````` #include using namespace std; const int lucky=6; void introduction (); int findouthowmany (int x, int y, int z); int didtheproducthit (int x, int y); int main() { int x, y, z, data=1, total=0; introduction (); while (data <= 8) { cout<< "data set " << data <> x >> y >> z; total++; cout<< findouthowmany (x,y,z) << " numbers are equal to the lucky number 6" << endl; didtheproducthit (x,y); didtheproducthit (y,z); didtheproducthit (x,z); cout<< endl << endl; } cout<< total << " sets of three data values were entered and processed." << endl; return 0; } // introdcution has no parameters and will print out a message on top of the program explaining what it will do. void introduction() { cout<< My lucky number is 6. This program will end when there are at least eight sets of data values or if the user types in all negative numbers." << endl << endl; return; } //findouthowmany will determine how many of the three integers: int x, y, and z //are equal to the lucky number. it will print how many of the three numbers are equal. int findouthowmany (int x, int y, int z) { int count=0; if (lucky==x) count++; if (lucky==y) count++; if (lucky==z) count++; if (x<0 && y<0 && z<0) // i am not sure how to end it after this. return count; } //didtheproducthit will determine whether or not the product of parameter x and y values //equal the lucky number and it will print a comment saying whether or not they multiply to 6. //this function will repeat three times in the main function. int didtheproducthit (int x, int y) { if (lucky==x*y) cout<< x << " and " << y << " multiply to 6" << endl; else cout<< x << " and "<< y << " do not multiply to 6" << endl; return 0; } ``````
Last edited on
i have to make it so that if the user types in all three integers negative the program will end right away and the number of data sets that were run appear at the bottom. how would i do that? thanks
Last edited on
@iky

This is one way. I'm sure there are more elegant ways of doing it, but this is probably the easiest to understand

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798`` ``````#include "stdafx.h" // Used with MS Visual Studio. Leave off if using something different #include using namespace std; const int lucky=6; void introduction(); int findouthowmany (int data, int Dataset[][3]); int didtheproducthit (int x, int y); int main() { int x, y, z, data=0, total; int Dataset[8][3]; introduction(); do { cout<< "Type three integers" << endl; cin >> x >> y >> z; Dataset[data][0]=x, Dataset[data][1]=y, Dataset[data][2]=z; total = findouthowmany (data, Dataset); cout<< total << " numbers are equal to the lucky number 6" << endl; didtheproducthit (x,y); didtheproducthit (y,z); didtheproducthit (x,z); cout<< endl << endl; data++; } while (data < 8 && total >= 0); if(total <0) { cout << "Three negative numbers were entered. Program is ending.." << endl; data--; // Subtract one since the negative inputs terminate the program. } cout<< data << " sets of three data values were entered and processed." << endl; cout << "They were..." << endl; for ( int a=0;a

i.e., DidTheProductHit or did_the_product_hit
Now as the case os closed, its funny, rollie, i read your spaceless comment very fine! just as spaced comments!
@whitenite1.
thank you so much but im not sure if i can use dataset yet because my prof hasnt taught it yet and i dont even know what that is. he is the type of guy that will take points off cause we didnt learn it yet. is there another way of doing this without dataset. thank you.
Last edited on
@iky

Dataset is just the name I gave the array. You haven't learned about arrays yet? The array was so you could show all the sets of numbers that were entered. I guess you don't really need that.
Anyway, I redid the program so as to NOT use an array. This is probably more of what you had in mind to begin with.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192`` ``````#include using namespace std; const int lucky=6; void introduction(); int findouthowmany (int x, int y, int z); int didtheproducthit (int x, int y); int main() { int x, y, z, data=0, total; introduction(); do { cout<< "Type three integers" << endl; cin >> x >> y >> z; if(x >=0 && y >=0 && z >=0) { total = findouthowmany (x, y, z); cout<< total << " numbers are equal to the lucky number 6" << endl; didtheproducthit (x,y); didtheproducthit (y,z); didtheproducthit (x,z); cout<< endl << endl; data++; } else total = -1; } while (data < 8 && total >= 0); if(total <0) { cout << "Three negative numbers were entered. Program is ending.." << endl; } cout<< data << " sets of three data values were entered and processed." << endl; return 0; } // introduction has no parameters and will print out a message on top of the program explaining what it will do. void introduction() { cout<< "My lucky number is 6. This program will end when there are at least eight\nsets of data values or if the user types in all negative numbers." << endl << endl; // return; Not really needed. Returns on its own when function ends } //findouthowmany will determine how many of the three integers: int x, y, and z //are equal to the lucky number. it will print how many of the three numbers are equal. int findouthowmany (int x, int y, int z) { int count=0; if(x<0 && y<0 && z<0) { count = -1; return count; } if (lucky==x) count++; if (lucky==y) count++; if (lucky==z) count++; return count; } //didtheproducthit will determine whether or not the product of parameter x and y values //equal the lucky number and it will print a comment saying whether or not they multiply to 6. //this function will repeat three times in the main function. int didtheproducthit (int x, int y) { if (lucky==x*y) cout<< x << " times " << y << " multiply to 6" << endl; else cout<< x << " times "<< y << " do not multiply to 6" << endl; return 0; } ``````
the new program you wrote stops the program when you enter in one negative number. i want it to end only if the user types in all three negative numbers.
Sorry iky. After the changes, I didn't check it would work with only 3 negatives. The problem was I checked if all 3 were zero or over, so of course it failed when even one, wasn't.

This one is correct. ( I checked ;) )

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135`` ``````// 3 Negative Numbers.cpp : main project file. #include "stdafx.h" #include #include using namespace std; /* bool numbers(); int main() { bool Neg_Num=false; do { cout << endl << "3 Negative numbers to end program"<< endl << endl << "Enter 3 positve numbers... "; Neg_Num = numbers(); }while(!Neg_Num); cout << endl << endl << "I see you are ending the program. Good-bye!!" << endl << endl; return 0; } bool numbers() { bool negative = false; int x,y,z; int sum; cout << endl << "X = "; cin >> x; cout << "Y = "; cin >> y; cout << "Z = "; cin >> z; if(x<0 && y<0 && z<0) { negative=true; return negative; } sum = x+y+z; cout << "The sum of " << x << ", " << y << " and " << z << " equal " << sum; return negative; }*/ const int lucky=6; void introduction(); int findouthowmany (int x, int y, int z); int didtheproducthit (int x, int y); int main() { int x, y, z, data=0, total; introduction(); do { cout<< "Type three integers" << endl; cin >> x >> y >> z; total = findouthowmany (x, y, z); if (total != -1) { cout<< total << " numbers are equal to the lucky number 6" << endl; didtheproducthit (x,y); didtheproducthit (y,z); didtheproducthit (x,z); cout<< endl << endl; data++; } } while (data < 8 && total >= 0); if(total <0) { cout << endl << "Three negative numbers were entered. Program is ending.." << endl; } cout<< data << " sets of three data values were entered and processed." << endl; return 0; } // introdcution has no parameters and will print out a message on top of the program explaining what it will do. void introduction() { cout<< "My lucky number is 6. This program will end when there are at least eight\nsets of data values or if the user types in all negative numbers." << endl << endl; // return; Not really needed. Returns on its own when function ends } //findouthowmany will determine how many of the three integers: int x, y, and z //are equal to the lucky number. it will print how many of the three numbers are equal. int findouthowmany (int x, int y, int z) { int count=0; if(x<0 && y<0 && z<0) { count = -1; return count; } if (lucky==x) count++; if (lucky==y) count++; if (lucky==z) count++; return count; } //didtheproducthit will determine whether or not the product of parameter x and y values //equal the lucky number and it will print a comment saying whether or not they multiply to 6. //this function will repeat three times in the main function. int didtheproducthit (int x, int y) { if (lucky==x*y) cout<< x << " times " << y << " multiply to 6" << endl; else cout<< x << " times "<< y << " do not multiply to 6" << endl; return 0; } ``````
Last edited on
Topic archived. No new replies allowed.