Desolate Carnage
 
@ Task
Archived | Views: 592 | Replies: 19 | Started 15 years, 4 months ago
 
#569877 | Fri - Jul 10 2009 - 15:33:07
Group: Guest
Posts: 5,799
Joined: May 26 2008
Contact: Offline PM
Points: 109.60
how do i make it in C++ for it to read positive integers only

problem:
Write a short C++ program that will read positive integers entered by the user until the value -1 is read, then print the sum of the values. If the user enters a negative number (other than -1), display an error message and ask them to enter another number.

Hint - use nested while loops to handle the input. Also, use a variable to accumulate the sum of the input values, and at the end of the program, display the largest number the user entered. (See Practice Program 3 for an example of finding the largest value)




going to use a while loop ofc
starting it like this

while (value != -1)
 
#569881 | Fri - Jul 10 2009 - 15:36:47
Group: Members
Posts: 74,76940k
Joined: Aug 5 2007
Contact: Offline PM
Points: 7,730.25 $
is value the variable you declared for the input
 
#569882 | Fri - Jul 10 2009 - 15:37:34
Group: Guest
Posts: 5,799
Joined: May 26 2008
Contact: Offline PM
Points: 109.60
ya
 
#569883 | Fri - Jul 10 2009 - 15:39:25
Group: Members
Posts: 74,76940k
Joined: Aug 5 2007
Contact: Offline PM
Points: 7,730.25 $
k, declare a variable for the sum, declare a variable for the largest integer entered

in the while loop, sum = sum + value; (i dont remember syntax, but you get the point)
if statement for, if value "greater than" largestentered, largestentered = value

This post has been edited by randomtask on Fri - Jul 10 2009 - 15:39:30
 
#569884 | Fri - Jul 10 2009 - 15:41:36
Group: Guest
Posts: 5,799
Joined: May 26 2008
Contact: Offline PM
Points: 109.60
so far

while (value != -1) {
cout << "Enter numbers";
cin >> value;
if (value != -1) {
total = total + value;
count = count + 1;
}
}
 
#569886 | Fri - Jul 10 2009 - 15:44:39
Group: Members
Posts: 74,19840k
Joined: Oct 25 2006
Contact: Offline PM
Points: 6,883.75 $ $
poop
 
#569887 | Fri - Jul 10 2009 - 15:44:40
Group: Guest
Posts: 5,799
Joined: May 26 2008
Contact: Offline PM
Points: 109.60
while (value != -1) {
cout << "Enter numbers";
cin >> value;
if (value != -1) {
total = total + value;
count = count + 1;
if (value > largest)
largest = value
}
}
 
#569888 | Fri - Jul 10 2009 - 15:45:08
Group: Members
Posts: 74,76940k
Joined: Aug 5 2007
Contact: Offline PM
Points: 7,730.25 $
Quote (boblong11 @ Fri - Jul 10 2009 - 16:41:36)
so far

  while (value != -1) {
          cout << "Enter numbers";
          cin >> value;
          if (value != -1) {
            total = total + value;
            count = count + 1;
            }
}


doesnt say anywhere that you need to count the number of numbers entered

but you do need to keep track of largest number
 
#569889 | Fri - Jul 10 2009 - 15:45:57
Group: Members
Posts: 74,76940k
Joined: Aug 5 2007
Contact: Offline PM
Points: 7,730.25 $
Quote (boblong11 @ Fri - Jul 10 2009 - 16:44:40)
while (value != -1) {
          cout << "Enter numbers";
          cin >> value;
          if (value != -1) {
            total = total + value;
            count = count + 1;
                  if (value > largest)
                      largest = value
            }
}


thats better
 
#569890 | Fri - Jul 10 2009 - 15:46:21
Group: Guest
Posts: 5,799
Joined: May 26 2008
Contact: Offline PM
Points: 109.60
Quote (randomtask @ Fri - Jul 10 2009 - 16:45:08)
Quote (boblong11 @ Fri - Jul 10 2009 - 16:41:36)
so far

  while (value != -1) {
          cout << "Enter numbers";
          cin >> value;
          if (value != -1) {
            total = total + value;
            count = count + 1;
            }
}


doesnt say anywhere that you need to count the number of numbers entered

but you do need to keep track of largest number


true that
 
#569891 | Fri - Jul 10 2009 - 15:46:47
Group: Guest
Posts: 5,799
Joined: May 26 2008
Contact: Offline PM
Points: 109.60
still need to count only positive numbers
thats what i have problem with
 
#569893 | Fri - Jul 10 2009 - 15:51:03
Group: Members
Posts: 74,76940k
Joined: Aug 5 2007
Contact: Offline PM
Points: 7,730.25 $
Quote (boblong11 @ Fri - Jul 10 2009 - 16:46:47)
still need to count only positive numbers
thats what i have problem with


nest another If in there right after the while for value greater than zero
 
#569895 | Fri - Jul 10 2009 - 15:56:52
Group: Guest
Posts: 5,799
Joined: May 26 2008
Contact: Offline PM
Points: 109.60
wow im retarded
i was reading positive as even this whole time

this program is cake then nvm
 
#569899 | Fri - Jul 10 2009 - 16:12:17
Group: Guest
Posts: 5,799
Joined: May 26 2008
Contact: Offline PM
Points: 109.60
it's displaying both cout's when launching the program


while (value != -1) {
if (value > 0) {
cout << "Invalid entry, enter another number ";
}
cout << "Enter numbers";
cin >> value;
if (value != -1) {
total = total + value;
if (value > largest)
largest = value;
}
}
 
#569900 | Fri - Jul 10 2009 - 16:13:14
Group: Members
Posts: 74,76940k
Joined: Aug 5 2007
Contact: Offline PM
Points: 7,730.25 $
Quote (boblong11 @ Fri - Jul 10 2009 - 17:12:17)
it's displaying both cout's when launching the program


while (value != -1) {
          if (value > 0) {
            cout << "Invalid entry, enter another number ";
                  }
          cout << "Enter numbers";
          cin >> value;
          if (value != -1) {
            total = total + value;
                  if (value > largest)
                      largest = value;
}       
}


greater than needs to be less than
 
#569901 | Fri - Jul 10 2009 - 16:20:58
Group: Loser
Posts: 539
Joined: Sep 20 2008
Contact: Offline PM
Points: Locked
it is what it is
 
#569908 | Fri - Jul 10 2009 - 16:49:38
Group: Guest
Posts: 5,799
Joined: May 26 2008
Contact: Offline PM
Points: 109.60
//Declerations
int total = 0;
int largest =0;
int value;


//Process
while (value != -1) {
if (value < 0) {
cout << "Invalid entry, ";
}
cout << "Enter numbers: ";
cin >> value;
if (value != -1) {
total = total + value;
if (value > largest)
largest = value;
}
}

//Display
cout << "The Largest is: " << largest << endl;
cout << "The sum is: " << total << endl;
 
#569909 | Fri - Jul 10 2009 - 16:53:47
Group: Guest
Posts: 5,799
Joined: May 26 2008
Contact: Offline PM
Points: 109.60
&& value > 0
 
#569910 | Fri - Jul 10 2009 - 16:58:11
Group: Members
Posts: 74,76940k
Joined: Aug 5 2007
Contact: Offline PM
Points: 7,730.25 $
Quote (boblong11 @ Fri - Jul 10 2009 - 17:53:47)
&& value > 0


thats what i had been thinking
 
#569912 | Fri - Jul 10 2009 - 17:34:09
Group: Guest
Posts: 12,45510k
Joined: Mar 24 2008
Contact: Offline PM
Points: 4,371.10
7
Archived | Views: 592 | Replies: 19 | General Archive - 2009 Topic List
 
Quit the Internet