Thread: Noobie question

  1. #1
    Noobie
    Guest

    Noobie question

    The code is below and from this sites tutorials. It is a multiplication program. But it has bugs, if I type a letter it will crash or make some error, likewise if I type a large number it will have problems. Isn't there someway to get it to output an error message and ask me to try again?

    #include <iostream.h>



    int mult(int x, int y);



    int main()

    {

    int x, y;

    cout<<"Please input two numbers to be multiplied: ";

    cin>>x>>y;

    cout<<"The product of your two numbers is "<<mult(x, y);

    return 0;

    }

    int mult(int x, int y)

    {

    return x*y;

    }

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    An int cannot hold a char value such as a letter, and ints can only hold so much data before overflow errors occur. You can prevent it by making sure the input is less than or equal to MAX_INT in values.h or something

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    I have no problems with your code, try this:

    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int mult(int x, int y);
    
    int main()
    {
    	int x, y;
    
    	cout<<"Please input two numbers to be multiplied: ";
    
    	cin>>x>>y;
    
    	cout<<"The product of your two numbers is "<<mult(x, y);
    
    	getch();
    	return 0;
    }
    
    int mult(int x, int y)
    {
    	return x*y;
    }
    PS: Use code /code tags between []

  4. #4
    Noobie
    Guest

    IM A NOOB

    whatever the reply was made very little no sense I AM A NOOB I understand that an integer isnt a character-however instead of this program saying

    An integer is not a character! Please try again!

    it will crash

    I have no idea what a MAX_INT is or what a values.h is

    all i wanna do is have it say

    if input != number
    then
    try again

    if input > toobig
    then
    i donno do something besides crash

  5. #5
    noobie
    Guest
    as for the second example I cant compile it the compiler says that a namespace name is expected i am getting lots of errors like that from other peoples tutorials for instance in the book i have it talks about using
    #include "string6.h" which it never says what that is and it never says what to do if my compiler says that it doesnt know what that is

  6. #6
    noobie
    Guest
    and what does PS: Use code /code tags between [] mean? i have not a clue

  7. #7
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

  8. #8
    Noobie
    Guest

    more noobie post

    here is a simpler program with the same problem - inputting a letter or large number causes it to crash or have some outrageous error

    also the name unregistered seems to be registered which presents a problem for noobies

    Code:
    #include <iostream.h>
    
    int main()
    
    {
      int i;
    
      char thisisanumber[256];
    
      cout<<"Please enter a number:";
    
      cin>>thisisanumber;
    
      cout << "You entered: ";
    
      i = 0;
    
      while (thisisanumber[i] >= '0' && thisisanumber[i] <= '9')
      {
        cout << thisisanumber[i];
    	i++;
      }
    
      cout << endl;
    
      return 0;
    
    }

  9. #9
    Registered User codingmaster's Avatar
    Join Date
    Sep 2002
    Posts
    309
    U can't enter a char, if u have declerated an int!!!!

  10. #10
    Noobie
    Guest
    Thats just it! I CAN enter a char, if u run these programs and type a letter instead of a number, it will try to use the letter when it should say, "I didnt ask for a letter, I asked for a number! Please try again!"

  11. #11
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Well, you're right, Noobie, real world programs need to be bullet-proof (or user-proof"). These types of protections are often left out of example programs because it complicates them, making it harder to see the point of the example. C++ provides "exception handling" when you want to get fancy. Most of my programs have a default value/choice, which is used it the user just presses ENTER, or enters something invalid.

  12. #12
    noobie
    Guest
    well i guess i want an example of a real-world program that does what these programs do.

  13. #13
    Noobie
    Guest
    I think I partially solved it-the below code has the user choose between 1 and 2-with a different choice resulting in a re-request for choices-however, the above question about large numbers still remains unanswered

    Code:
    
    #include <iostream.h>			
    
    int main()
    {
    
      int choice;
      int room;
    
      while(x==0)	
    
     {
    
        cout<<"Please choose a room (1 or 2): ";	
    
        cin>>choice;	  
    
    if(choice==1)
    x=x++
    else if(choice==2)
    x=x++
    else
    x=x        		
      } 				
    
      if(choice==1)				
    
      {
    
      		room=1;
         cout<<"You enter a room\n";    
    
      }
    
      else if(choice==2)		
    
      {
      		room=2;
    
         cout<<"You are in room 2\n";		
    
      }
    
      else
    
      {
    
        cout<<"You should not see this.\n";
    
      }
    
      if (room==1)
      {
      cout<<"Yep";
      }
      else if (room==2)
      {
      cout<<"Nope";
      }
      else
      {
      cout <<"I need to know how to ask them to plz enter a proper room choice, but how to do that? i don't know.";
      }
      return 0;
    
    }

  14. #14
    Noobie
    Guest
    However, the program crashes if a letter is entered....How can I prevent that?

  15. #15
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by Noobie
    However, the program crashes
    Define crash, does it just quits? does crash windows? what?

    You must return a value from main, in this case an int

    Code:
     int main()
    {
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. A fourth noobie question - namespace std?
    By Noobie in forum C++ Programming
    Replies: 24
    Last Post: 08-12-2005, 02:10 PM
  3. another noobie question
    By noobie in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 02:04 PM
  4. A third noobie question
    By Noobie in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2003, 12:53 AM