#Need help to do my cpp assignment
1 messages · Page 1 of 1 (latest)
This is the homework question
#include <iostream>
#include <iomanip>
using namespace std;
const int SENTINEL = -999;
int main()
{
int sum=0;
int numVal;
int average;
while(numVal!=-999)
{
cout <<" Enter a number ";
cin >> numVal;
sum=sum + numVal;
}
cout << "The sum of the vlaues entereed is" << sum << endl;
cout << "The number of values entered is" << numVal << endl;
average = sum/count;
cout << "The average of values entered is" << average << endl;
return 0;
} '''
Can you send real screenshots instead of photos. It's really hard to read.
my code
Write a complete program in C++ to have the user enter numbers until a sentinel of -999 is entered.
Count the number of values entered, total all the values entered, and calculate the average of the entered numbers.
Output all numbers entered, their total and their average, clearly labeled
Use a while loop.
I know my code is probably very off im a little confused on this one
Send you code like bellow
```cpp
your code here
```
PS. You can edit your previous message
I know I'm annoying but I'M trying to help you increase your chances of getting an answer
Asking question correctly is very important.
No im very grateful for you bro
i understand
ngl this week i missed a little but im still doing good i just need some help
Need help to do my cpp assignment
Can you elaborate on what you don't understand? Because your code seems fine except for a few things missing and that you forgot to define count
Pro tip: You can do sum += numVal instead of sum = sum + numVal
Im going to finish this assignment tomorrow I think I have it but Im about to finish another lab and i have a question on that assignment.
Write an interactive C++ program to have a user input their three initials and the length of three sides of a triangle. Allow the user to enter the sides in any order.
Determine if the entered sides form a valid triangle. The triangle may not have negative sides. The sum of any two sides must be greater than the third side to form a valid triangle.
Determine if the entered sides are part of a scalene, isosceles, or equilateral triangle. (Scalene – three sides different lengths; isosceles – 2 sides same length; equilateral – 3 sides same length.)
Triangle Data:
Initials entered: A B C
Sides entered: 3 4 5
Triangle type: Scalene
Right Triangle ? Right Triangle
this is what output should look like
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
string TriName;
int Side1;
int Side2;
int Side3;
cout << showpoint << fixed << setprecision(2) << endl;
cout << "Please enter three initals dedicated to the three sides of a triangle. Do not put a space bewteen characters: ";
cin >> TriName;
cout << endl;
cout << "Please enter three numbers for each side of the triangle. Do not enter a negative number" << endl;
cout << "Side 1: ";
cin >> Side1;
cout << "Side 2: ";
cin >> Side2;
cout << "Side 3: ";
cin >> Side3;
while (Side1 + Side2 <= Side3 || Side1 + Side3 <= Side2 || Side3 + Side2 <= Side1) {
if (Side1 != Side2 !=Side3) {
cout << "Scalene Triangle";
}
if (Side1 = Side2 = Side3) {
cout << "Equilateral Triangle";
}
if (Side1 == Side2 || Side1 == Side3 || Side2 == Side3) {
cout << "Isosceles";
}
}
cout << "Data entered for Triangle is not valid, make sure you have input no negative numbers";
} ```
so my question is I know I have the wrong loop setting Im not sure which one is correct to start verifying what type of triangle it is
as of right now it os only doing the data entered is not valid route I want that if only the data entered is not valid
yeah idk how to define count but I think I have that assignment I will try again later
Of course you know how to define count. int count = 0;
That's all
Then you need to make it increment when a new number is entered.
As for this, this loop is simply misplaced. It should look more like this
Oh insee
while condition
display message
input
end while
if is scalene
display message
else if is equilateral
display message
else if is isosceles
display message
end if
Btw, you should pay more attention on how you write you code. Writing cleaner code help debugging.
I will tank you sorry for late respone I had a quiz
NP
so should I put the side input inside the first while condition is that what you mean
let me see
slightly confused what should be displayed and input in the while condition. Ugh i feel so stupid not being able to find this out
yeah im a little pressed on time this time completley my fault. But still I try to but any tips you have to keeping my code cleaner
oh also
it should be > right
yess
that worked not it goes through
now*
- Separate with space your code in logical sections. Ex. (bellow)
// Section where you define your varibles
string triName;
int side1;
int side2;
int side3;
// Section where you get the inputs of your user
cout << showpoint << fixed << setprecision(2) << endl;
cout << "Please enter three initals dedicated to the three sides of a triangle. Do not put a space bewteen characters: ";
cin >> triName;
cout << endl;
- Space operations. Ex.
x = 2 * 5,numVal != -999. - Avoid naming your variables with abbreviation.
- In CPP variable names shouldn't start with capital letters
ok so now my output just infinitely repeats. How do I end my while loop because it just a while i didn't put anything in it
Unless there's something you changed there's only one while loop and yes the inputs should go there.
I will keep these in mind
The code that finds which type of triangle it is shouldn't be there tho
yes that should be outside the loop correct
Hence why I separated them in the pseudo code I've sent
# Get the user input
while condition
display message
input
end while
# Find which type it is
if is scalene
display message
else if is equilateral
display message
else if is isosceles
display message
end if
interesting so, I have the input inside while even though I could input something that would not run it
I assumed you input then the while runs because the value matches with its task
oooh
#include <iomanip>
#include <string>
using namespace std;
int main() {
// Section where you define your varibles
string TriName;
int Side1;
int Side2;
int Side3;
cout << showpoint << fixed << setprecision(2) << endl;
cout << "Please enter three initals dedicated to the three sides of a triangle. Do not put a space bewteen characters: ";
cin >> TriName;
cout << endl;
// Section where you get the inputs of your user
while (Side1 + Side2 > Side3 || Side1 + Side3 > Side2 || Side3 + Side2 > Side1) {
cout << "Please enter three numbers for each side of the triangle. Do not enter a negative number" << endl;
cout << "Side 1: ";
cin >> Side1;
cout << "Side 2: ";
cin >> Side2;
cout << "Side 3: ";
cin >> Side3;
break;
}
if (Side1 != Side2 !=Side3) {
cout << "Scalene Triangle";
}
else if (Side1 = Side2 = Side3) {
cout << "Equilateral Triangle";
}
else if (Side1 == Side2 || Side1 == Side3 || Side2 == Side3) {
cout << "Isosceles";
}
cout << "Data entered for Triangle is not valid, make sure you have input no negative numbers";
} ```
this is what I have now. My output is "Scalene TriangleData entered for Triangle is not valid, make sure you have input no negative numbers"
so should that final cout be an else if
It would be a good moment to use do-while instead to simplify your code.
ah
Also the break will screw up your loop
Here's a correctly formatted version
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
string triName;
int side1;
int side2;
int side3;
cout << showpoint << fixed << setprecision(2) << endl;
cout << "Please enter three initals dedicated to the three sides of a triangle. Do not put a space bewteen characters: ";
cin >> triName;
cout << endl;
// Section where you get the inputs of your user
while (side1 + side2 > side3 || side1 + side3 > side2 || side3 + side2 > side1) {
cout << "Please enter three numbers for each side of the triangle. Do not enter a negative number" << endl;
cout << "Side 1: ";
cin >> side1;
cout << "Side 2: ";
cin >> side2;
cout << "Side 3: ";
cin >> side3;
}
if (side1 != side2 !=side3) {
cout << "Scalene Triangle";
} else if (side1 = side2 = side3) {
cout << "Equilateral Triangle";
} else if (side1 == side2 || side1 == side3 || side2 == side3) {
cout << "Isosceles";
}
cout << "Data entered for Triangle is not valid, make sure you have input no negative numbers";
}
Yea it does a really big different
It's also way easier to think about your code and debug it
ur crazy. Yeah on my online coding work and stuff im going to try to implement these things to help
how do i make it so it wont only output scalene
is it = or == to verify equality between to inputs
im so close sorry for bugging you
Instead of having this side1 != side2 !=side3 as the first condition, remove it and make side1 = side2 = side (which btw should be side1 == side2 && side1 == side). The, it's basically Scalene if it's not any of the two other condition so just put it in the else
if (side1 == side2 == side3) {
cout << "Equilateral Triangle";
} else if (side1 == side2 || side1 == side3 || side2 == side3) {
cout << "Isosceles Triangle";
} else {
cout << "Scalene Triangle";
}
You understand why?
yes i do, but if i wanted to could i do scalene second
Not really
then i do not
I mean, theoretically you could but that might require some code duplication and add more checks and stuff
i think i have an idea, is it because when i do that is it setting the things to always not equal
It would be possible but it would be a lot more work for nothing
It's not setting anything. It's because if the sides are not equals (which is almost always the case) than side1 != side2 != side3 is true and thus the first if is executed
Make sense?
yes
yay. ok now my error check was pointless because the final message will always send
gonna go back and read to see how to error check for negative value
Hint it needs to go with the input section
aaaah i saw it in the books but now i cant find it
oooh now i see what u mean with the do while loops
then put it at the end
f me**
srry
also the first one equilateral will not work
aaaah i just want to be done
im losing it
how to make equilateral work and error check pls no more questions after
I think it should be side1 == side2 || side2 == side3 || side3 == side1 and for the error message simply add a if at the end of the loop that checks if data entered is valid and print the message if it isn't
if (side1 + side2 !> side3 || side1 + side3 !> side2 || side3 + side2 !> side1) {
cout << "Data entered for Triangle is not valid, make sure you have input no negative number
this wont work will it
oh i can just do it if a value is 0 or negative
No this !> doesn't exit
oh
What would be the contrary of >?
Show me the error
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
string triName;
int side1;
int side2;
int side3;
cout << showpoint << fixed << setprecision(2) << endl;
cout << "Please enter three initals dedicated to the three sides of a triangle. Do not put a space bewteen characters: ";
cin >> triName;
cout << endl;
// Section where you get the inputs of your user
while (side1 + side2 > side3 || side1 + side3 > side2 || side3 + side2 > side1) {
cout << "Please enter three numbers for each side of the triangle. Do not enter a negative number" << endl;
cout << "Side 1: ";
cin >> side1;
cout << "Side 2: ";
cin >> side2;
cout << "Side 3: ";
cin >> side3;
break;
}
if (side1 < 0 || side2 < 0 || side3 < 0) {
cout << "Data entered for Triangle is not valid, make sure you have input no negative numbers";
}
else if (side1 == side2 == side3) {
cout << "Equilateral Triangle";
}
else if (side1 == side3 || side3 == side1 || side2 == side3) {
cout << "Isosceles Triangle";
}
else {
cout << "Scalene Triangle";
}
return 0;
}
no error now only the equillatoral thing i keep trying diffrent values
it works now fine i just need to make sure if all are euqal that pops up but it still does the other
yes
for example 341 works 333 will be isocolese not equilateral
and 323 will be isosceles
Let me write you something
;compile
#include <iostream>
int findTriangleType(int a, int b, int c) {
if (a == b && b == c && c == a) {
std::cout << "Equilateral Triangle" << std::endl;
} else if (a == b || b == c || c == a) {
std::cout << "Isosceles Triangle" << std::endl;
} else {
std::cout << "Scalene Triangle" << std::endl;
}
return 0;
}
int main() {
findTriangleType(3, 3, 3);
findTriangleType(3, 2, 3);
findTriangleType(3, 4, 1);
return 0;
}
Equilateral Triangle
Isosceles Triangle
Scalene Triangle
oh the &
Yip
main.cpp:36:20: error: comparison between pointer and integer ('int' and 'void *')
else if (side1 == && side2 == && side3) {
~~~~~ ^ ~~~~~~~~
main.cpp:36:26: error: use of undeclared label 'side2'
else if (side1 == && side2 == && side3) {
^
main.cpp:36:38: error: use of undeclared label 'side3'
else if (side1 == && side2 == && side3) {
^
3 errors generated.
make: *** [Makefile:17: main.o] Error 1
exit status 2
wat that mean
nvm
bro is an angel is disguise
how long have you been coding
How for the issue about the error message
while (side1 + side2 < side3 || side1 + side3 < side2 || side2 + side3 < side1) {
cout << "Please enter three numbers for each side of the triangle. Do not enter a negative number" << endl;
cout << "Side 1: ";
cin >> side1;
cout << "Side 2: ";
cin >> side2;
cout << "Side 3: ";
cin >> side3;
if (side1 + side2 < side3 || side1 + side3 < side2 || side2 + side3 < side1) {
cout << "Data entered for Triangle is not valid, make sure you have input no negative numbers";
}
}
You forgot to compare it to the other variable. You did side1 == && ... when it should be side1 == side2 && ...
Oh sry I just saw you nvm
hahaha thanks
I've been coding for about 10-12 years
Professionally for about 5 years
I don't usually write C++ tho XD
Haha yeah I want to learn Java and Python
Thats crazy I hope one day im as fluent as you. You definitely give me motivation to keep learning and trying bro🙏🏾
Two also great language. I mostly work with those 2.
I'm sure you will. Happy to hear that it motivates you. Don't give up you got this!