Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F102883856
ch-02-ex-03-solution.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, Feb 25, 04:41
Size
1 KB
Mime Type
text/x-c
Expires
Thu, Feb 27, 04:41 (2 d)
Engine
blob
Format
Raw Data
Handle
24405538
Attached To
R1106 Programming Concept Rouaze
ch-02-ex-03-solution.cpp
View Options
/*
* chapter-02-exercise-04.cpp
*
* Using the while loop
*
* Created on: Aug 19, 2012
* Author: Radu Popescu <radu.popescu@epfl.ch>
*/
#include <iostream>
int main(int argc, char* argv[])
{
int number = 0, sum = 0;
/*
* Point 1. Terminate reading numbers when -1 is input by the user
*/
while (number >= 0) {
sum += number;
std::cout << "Please input numbers. Terminate with -1 :";
std::cin >> number;
}
std::cout << "The sum of the numbers is: " << sum << std::endl;
/*
* Point 2. Terminate reading numbers when -1 is input by the user
* or when sum exceedes 100.
*/
do {
std::cout << "Please input numbers. Terminate with -1 :";
std::cin >> number;
if (number != -1) sum += number;
} while ((number >= 0) && (sum <= 100));
std::cout << "The sum of the numbers is: " << sum << std::endl;
/*
* Point 3. Reset sum by inputting -2
*/
do {
std::cout << "Please input numbers. Terminate with -1. Reset with -2 :";
std::cin >> number;
switch (number) {
case -1:
break;
case -2:
sum = 0;
break;
default:
sum += number;
break;
}
} while ((number >= 0) && (sum <= 100));
std::cout << "The sum of the numbers is: " << sum << std::endl;
return 0;
}
Event Timeline
Log In to Comment