C++ code f0r this plz!

Jun 26, 2009 at 12:06pm
C++ masterz plz help me ab0ut this questi0n., this is my assignment in 0ur c0mputer pr0gramming that have to pass 0n m0nday june,29,2009... H0w to c0de this? My pr0f want like this: 0nly put 1 n0. Like 9, 11 and s0 on... Ex. If u put less than to 10 this will be print, example y0u put 5 which is less than to 10 the answer will be 5+4+3+2+1 and if y0u put 3 answer will be 3+2+1... That's all please answer... I'm using turb0 C++ and this are s0me c0des ive using:
#include <iostream.h>
void main()
cout
cin
if statement
else if... H0pe y0u can answer i'll w8 4 that thanks
Jun 26, 2009 at 12:20pm
You are going to have to make a better attempt at it first before anyone is going to help you. We aren't a homework service.

Please post your non-working, but compiling code.
Jun 26, 2009 at 12:29pm
Off Topic: It scares me a little that you lack an 'o' key...
Jun 26, 2009 at 12:46pm
Sir its ok for me s0ri im new... But can u give me s0me example ok clues 4 this? Tnx a lot
Jun 26, 2009 at 1:09pm
#include <i0stream>

You don't even try to clarify your words.
Jun 26, 2009 at 1:22pm
Ok, I'm probably being too generous here, given that I can't be sure you spent more than the 2 minutes it took to type your original post on the project.

Compile and run this:

1
2
3
4
5
6
#include <iostream>

int main() {
    for( int i = 0; i < 10; ++i )
        std::cout << "i = " << i << std::endl;
}


What you need is only one or two steps beyond that. You need to prompt the user to enter a number (use cout), read the number (use cin), then add up all numbers from 1 to the number they entered (use a for() loop that counts from 1 to that number and add).

Jun 26, 2009 at 2:02pm
closed account (z05DSL3A)
I'm feeling generous … possibly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <sstream>

int main() 
{
    std::string input = "";
    int number(0);

    while (true) 
    {
        std::cout << "Please enter a valid number (1 to 10): ";
        std::getline(std::cin, input);
   
        std::stringstream ss(input);
        if ((ss >> number) && (number <=10) && (number > 0))
            break;
        std::cout << "Invalid number, please try again" << std::endl;
    }
    std::cout << "You entered: " << number << std::endl;

    int sum(0);
    for(int i = number; i != 0; --i)
    {
        sum +=i;
        std::cout << i << ((i > 1)? " + " : " = ");
    }
    std::cout << sum << std::endl;

    return EXIT_SUCCESS;
}
time to hit the books and work out what is going on...
Last edited on Jun 26, 2009 at 2:04pm
Jun 26, 2009 at 6:04pm
Maybe if you typed properly, we would understand what you were saying.
Jun 26, 2009 at 10:38pm
Thnx 4 d help guys ,..n0w i kn0w..., yes! Tnx again 4 d examples n clues masterz...
Topic archived. No new replies allowed.