#Need help to do my cpp assignment

1 messages · Page 1 of 1 (latest)

jade bough
#

Help with homework code

#

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;

} '''
polar forum
#

Can you send real screenshots instead of photos. It's really hard to read.

jade bough
#

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

polar forum
#

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.

jade bough
#

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

polar forum
#

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

jade bough
#

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

jade bough
polar forum
#

That's all

#

Then you need to make it increment when a new number is entered.

polar forum
jade bough
#

Oh insee

polar forum
#

Btw, you should pay more attention on how you write you code. Writing cleaner code help debugging.

jade bough
#

I will tank you sorry for late respone I had a quiz

polar forum
#

NP

jade bough
#

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

jade bough
#

oh also

#

it should be > right

#

yess

#

that worked not it goes through

#

now*

polar forum
# jade bough yeah im a little pressed on time this time completley my fault. But still I try ...
  1. 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;
  1. Space operations. Ex. x = 2 * 5, numVal != -999.
  2. Avoid naming your variables with abbreviation.
  3. In CPP variable names shouldn't start with capital letters
jade bough
#

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

polar forum
polar forum
#

The code that finds which type of triangle it is shouldn't be there tho

jade bough
#

yes that should be outside the loop correct

polar forum
#

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
jade bough
#

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

polar forum
jade bough
#

ah

polar forum
#

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";
} 
jade bough
#

ok i did that and it works now

#

damn bro ur formating is crazy

#

so readable

polar forum
#

Yea it does a really big different

#

It's also way easier to think about your code and debug it

jade bough
#

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

polar forum
#

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?

jade bough
#

yes i do, but if i wanted to could i do scalene second

polar forum
#

Not really

jade bough
#

then i do not

polar forum
#

I mean, theoretically you could but that might require some code duplication and add more checks and stuff

jade bough
#

i think i have an idea, is it because when i do that is it setting the things to always not equal

polar forum
#

It would be possible but it would be a lot more work for nothing

polar forum
#

Make sense?

jade bough
#

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

polar forum
jade bough
#

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

polar forum
#

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

jade bough
#

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

polar forum
#

No this !> doesn't exit

jade bough
#

oh

polar forum
#

What would be the contrary of >?

jade bough
#

<

#

i see

#

it would be then something like if side1 < 0 then

#

output error code

polar forum
#

Show me the error

jade bough
#

#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

polar forum
#

What?

#

Are you talking about finding the type of triangle?

jade bough
#

yes

#

for example 341 works 333 will be isocolese not equilateral

#

and 323 will be isosceles

polar forum
#

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;
}
limpid sparrowBOT
#
Program Output
Equilateral Triangle
Isosceles Triangle
Scalene Triangle
jade bough
#

oh the &

polar forum
#

Yip

jade bough
#

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

polar forum
#

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";
        }
    }
polar forum
#

Oh sry I just saw you nvm

polar forum
polar forum
#

Professionally for about 5 years

#

I don't usually write C++ tho XD

jade bough
#

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🙏🏾

polar forum
polar forum