Hello I am reather new in c++ and I try to solve some exercises but i got blocked. Can anyone help me with this?
The program should read in values until a negative value is read in. It should determine the largest value that was read in and display that value once the loop stops.
This program involves indefinite iteration using a sentential controlled loop. The sentential value is any negative integer. The fact that negative integers are sententials should help you determine how to initialize the variable that contains the largest integer.
I wrote this code but it gives me only the result 0:

#include<iostream>
using namespace std;

int main ()
 {
         int numar, biggest =0;
         cout<<"insert an integer ";
         while( numar > 0)
         cin>>numar;
        numar=biggest;
           if(numar<biggest)
           {
                            numar = biggest;
                            }
           cout<<biggest;
           system("pause");
           return 0;
           }

and just don'tknow what is wrong

Recommended Answers

All 7 Replies

delete line 10

delete line 10

same

The formatting leaves something to be desired.
Variable numar needs to be initialized to some value greaterthan 0
The program is missing { and } around that multi-line while statement
The if and assignment statements are backwards

int main ()
{
    int numar = 1, biggest =0;
    cout<<"insert an integer ";
    while( numar > 0)
    {
        cin>>numar;
        if(biggest < numar)
        {
            biggest = numar;
        }
    }
    cout<<biggest;
    system("pause");
    return 0;
}

I changed the code into this

#include<iostream>
using namespace std;

int main ()
 {
         int numar, biggest = 0;
         cout<<"insert an integer ";
         while( numar >= 0)
         cin>>numar;
           if(numar<biggest)
           {
                            biggest = numar;
                            }
           cout<<biggest;
           system("pause");
           return 0;
           }

and now if I enter the integers and the value -1 it gives me the result -1
I am closer but should give me the biggest value not the negative

Compare the program I posted with what You posted. You failed to add { and } round that while statement, and the comparison on line 10 is still wrong.

You need to learn to pay more attention to the logic of your programs.

Thank you very much
It works
It is really hard to learn alone only from books

>>It is really hard to learn alone only from books

Many of us learned that way too. Here at DaniWeb you are not alone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.