Thread: I have few questions about basic C++ programing

  1. #1
    Registered User
    Join Date
    Oct 2007

    I have few questions about basic C++ programing


    I wanted to find about few things that I didnt see in tutorials in this page.

    I've been working on very simple program for few hours, but I still cant figure out few things, can't find them either.

    This is the code I came up with.

    #include <iostream>
    using namespace std; 
    int main ()
          int a, b;
          float result;
          cout << "Please enter first number" << endl;                      
          cin >> a; 
          cout << "Please enter second number" << endl;
          cin >> b;
          cout << "Average is" << endl;
          result = (a + b)/2.0;  
          cout << result << endl;      
          system ("pause");
          return (0);
    is it possible to input unlimited amount of nummbers nut just number 1 + number 2?
    If yes then how ? for example 1+45+34
    And how can I change result function so it understands how many numbers I have inputed and calculates the average value?

    If I input a letter instead of nummber how can I make it so program says that it is invalid input value or something like that ?

    Thank you in advance,

    I read topics about posting and homework and I hope that I don't offend anyone with this topic

    P.S Sorry for any english mistakes !

  2. #2
    Registered User
    Join Date
    Jan 2007
    Euless, TX
    For what you are doing, you will have to enter one number at a time, but if you want to enter more than one number, you can use a "while" loop, say causing the loop to stop if a 0 is entered. You can also store all these numbers in an array and maintain your counter inside the while loop. As for checking if it's a number and not a char, you can use some built-in functions that are part of the standard libraries. I'll let you discover them. Any good book on C/C++ should have those functions listed somewhere in the book. Also, read about the "continue" statement. You may want to use it if your program finds a character and not a number.

  3. #3
    Registered User
    Join Date
    Oct 2007
    characters can work with the > and < operators. you could look at the input one character at a time and make sure it isn't a letter.
    //if the character is a number
    if( character >= '0' && character <= '9' )
    You can find an ASCII table online that shows the order

    once you have your number in string format, you can use atoi() to change it into an integer. search online for how to use it.
    Last edited by avatarofhope2; 10-17-2007 at 10:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. questions about arrays basic collision detection
    By The_Hermit in forum Game Programming
    Replies: 2
    Last Post: 07-23-2008, 11:29 AM
  3. Please help, basic unsigned conversion questions
    By ninjacookies in forum C Programming
    Replies: 3
    Last Post: 04-20-2005, 10:50 AM
  4. questions questions questions.....
    By mfc2themax in forum A Brief History of
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM