🔒 Closed C++ problem

Status
Not open for further replies.
Gawa mo ba yn boss or copy paste mo lng ?
May error kc. Btw ty :)
Ito try mo.


Code:
#include <iostream>
using namespace std;

int main ()
{
    int i = 0, sum = 0;
    int input;
    float ave;

    while(true)
    {
        cout<< i+1<< ".Enter a Grade (Enter negative integer to stop): ";
        cin>> input;

        if(input  < 0)
        {
            break;
        }
       
        i++;
        sum += input;
        cout << "Current Sum : " << sum << endl;
    }

    ave = sum /(i*1.0);
    cout << " Average : " << ave <<endl;

    return 0;
}
 
Ganito lang format ng loop.

while (parameter)
{
code
}

do
{
code
} while(bool)

for(statements)
{
code
}

walang
for(statemetns)
{
code
} while(bool)

gets mo na?
so alam mo function ng for loop and do while loop?ry
Ito try mo.


Code:
#include <iostream>
using namespace std;

int main ()
{
    int i = 0, sum = 0;
    int input;
    float ave;

    while(true)
    {
        cout<< i+1<< ".Enter a Grade (Enter negative integer to stop): ";
        cin>> input;

        if(input > -1)
        {
            i++;
            sum += input;
            cout << "Current Sum : " << sum << endl;
        }
        else
        {
            break;
        }
    }

    ave = sum /(i*1.0);
    cout << " Average : " << ave <<endl;

    return 0;
}
Thanks boss
 
Gawa mo ba yn boss or copy paste mo lng ?
May error kc. Btw ty :)

Gawa ko yan running smoothly skin, anong error lumalabas sayo?

Ganda nga ng flow nyan
Enter ng grade
Iadd yan sa sum initial value is 0
Pag negative nilagay na grade matatapos ang program
Ididivide ang sum depende sa dami ng grades input, loop

Iddisplay ang sum, total, average

Mkkta mo hnd lng main ang func pinaghiwalay ko para matutunan mo gumawa nyan, para kht mrmi kng function gwin mdli mo tawagin at hnd paulit ulit ang coding
 
A

Ano complicated dyan? Sobrang basic nga nyan

Gumawa lng ng other separate function para sa iloloop na code then call sa main func.
Mali kasi flow. Tinatawag mo yung Func1. Pero hindi nasasave yung value nung mga variable sa loob nung function na yun. Kung tatawagin ulit, magiinitialize ulit yung values ng mga yun. Na try mo ba yang code mo?
 
Mali kasi flow. Tinatawag mo yung Func1. Pero hindi nasasave yung value nung mga variable sa loob nung function na yun. Kung tatawagin ulit, magiinitialize ulit yung values ng mga yun. Na try mo ba yang code mo?
Oo naman nasasave values nyan, coded via dev c++ yn. Ivideo ko pa?
 
Oo naman nasasave values nyan, coded via dev c++ yn. Ivideo ko pa?
Mali nga.. Haha!
1.PNG
 
Then mali ang compiler.
Kung itatry mo yang code mo dito, incorrect pa rin.
You do not have permission to view the full content of this post. Log in or register now.
Kung sa dev c++ lang gagana code do you think tama yun?
And, the concept is the variables inside a function called are only temporary. after matapos yung task nun.

I see. May point ka. Kung hindi gumagana sa current compiler nya. O diyan, he/she can place the local variables to global variables then compile. Easy.
 
ang sagot bakit nag eeror.

while ( n > 0 ) ; <------

semi colon ang problema. close mo agad si while loop.


and checked also your code
hindi pwedeng gamitin si while loop with closing statement wtihout the do statement.
 
Simple solution for a simple problem

Instructions


Given problem:

Write a program written in c++ language to calculate the average of an unknown number of grade.

1. Prompts the user to enter a grade each time through the loop.
2. Update the sum and number of grade.
3. Prompts the user to enter a negative grade after the last valid grade.
4. Continue looping while the input grade is not negative.

Solution
Code:
#include <iostream>
using namespace std;
int main()
{
    float input = 0, grades = 0;
    int gradesCounter = 0;
    cout<<"Enter Negative Grade To Stop"<<endl;
    while(true)
    {
        cout<<"Enter Grade: ";
        cin>>input;
        if (input >= 0)
        {
            grades+=input;
            gradesCounter++;
        }
        else { break; }
    }
    cout<<"Total Grade/s: "<<gradesCounter<<endl;
    cout<<"Average: "<<(grades/gradesCounter);
    return 0;
}

Output
Enter Negative Grade To Stop
Enter Grade: 1
Enter Grade: 2
Enter Grade: 3
Enter Grade: 4
Enter Grade: 5
Enter Grade: 6
Enter Grade: -1
Total Grade/s: 6
Average: 3.5
 
Ito try mo.


Code:
#include <iostream>
using namespace std;

int main ()
{
    int i = 0, sum = 0;
    int input;
    float ave;

    while(true)
    {
        cout<< i+1<< ".Enter a Grade (Enter negative integer to stop): ";
        cin>> input;

        if(input  < 0)
        {
            break;
        }
      
        i++;
        sum += input;
        cout << "Current Sum : " << sum << endl;
    }

    ave = sum /(i*1.0);
    cout << " Average : " << ave <<endl;

    return 0;
}

Screenshot_2018-10-17-01-19-14.webp

di ako maka relate pero gusto kung matoto

yan result ts sa sample ni boss mrHazan
 
Status
Not open for further replies.

About this Thread

  • 40
    Replies
  • 2K
    Views
  • 9
    Participants
Last reply from:
siesta

Trending Topics

Online now

Members online
1,109
Guests online
1,076
Total visitors
2,185

Forum statistics

Threads
2,279,012
Posts
28,987,521
Members
1,226,696
Latest member
logitechgiant
Back
Top