Thread: assigning value

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    assigning value

    hi everybody! I'm new to programing in C++.
    I have a question..How I can assign to "x" values beetween 0 and 10.
    I can make " int 0<x<10;" or "int x=1,2,3,4,5,6,7,8,9;" or...
    please help me

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try a for loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    1
    This displays numbers from 0 to 10.
    If you want it to go from 1 to 10 just change "int x = 0" to "int x = 1".

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {
        for (int x=0; x<=10; x++)
        {
            cout << x << endl;
        }
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    I want to make a small game ..to guess the number beetween 1 an 10. I don't want to show the numbers 1,2,3...10. I know woh to make that . so?

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    search up:
    -if statements
    -cin


    edit: also you will need rand()

  6. #6
    Registered User slartie's Avatar
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    6
    I'm new to C++ and I consider myself a complete dunce when it comes to this language, but curiosity got the better of me and decided to give this a go, (even though I guess this will do the homework for the original poster).

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include topsecret
    
    using namespace std;
    
    int main ()
    {
        int number = 0, guess = 0;
        srand ( somemagic );
        number = rand() % fairydust;
    
        cout << "I'm thinking of a number. Can you guess which?" << endl;
    
        while (guess != guesswhat)
        {
            cout << "Pick a number : ";
            cin >> guess;
    
            if (number > guess)
                cout << "Too low" << endl;
            else if (number < guess)
                cout << "Too high" << endl;
            else
                cout << "You're a genious!" << endl;
        }
    }
    It took me a while to figure out how rand() worked, but coming from nothing to the above in 30 minutes is nothing less than amazing to me.
    Last edited by slartie; 10-17-2011 at 01:02 PM. Reason: obscuring a few obvious parts of the code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning something
    By Jasper in forum C Programming
    Replies: 14
    Last Post: 10-13-2009, 01:58 AM
  2. Assigning a pointer a value -- Still = 0?
    By rjg32 in forum C Programming
    Replies: 1
    Last Post: 02-05-2009, 12:16 AM
  3. Assigning probability
    By dalek in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2003, 08:26 PM
  4. Assigning
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-08-2001, 06:29 PM
  5. Assigning an ASCII value?
    By jackwoz in forum C Programming
    Replies: 4
    Last Post: 11-17-2001, 03:02 PM