Thread: getting weird answer when not using ".h"

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    26

    getting weird answer when not using ".h"

    i made a simple program that multiplies to numbers you enter. if you enter a letter the answer comes out to 0. all this time i was using <iostream.h>, so i decided to skip the .h and put using namespace std. after doing so it multiplied numbers fine, but when i enter a letter the answer would come out to something like 9852459. anyone know why???

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Can you show me the code?

    I think the behavior of the new iostreams differs from the old in that:
    In the old, if a >> operator couldn't parse a number it would read 0.
    In the new it reads undefined, thus the variable doesn't change its (random) initial value.

    cin.good() returns false if the operator failed to read the variable type.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    show the code. You can just make a safe-input setup so it only calculates the intended data type.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You can also tell the stream to throw an exception.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Student Forever! bookworm's Avatar
    Join Date
    Apr 2003
    Posts
    132
    I think you might be using the wrong variable type.Decide the range of input nos. that you desire and then use the proper variable type,such as ''unsigned long''
    Also,along with the code,state the compiler that u r using and its version.
    Use simple if..then or switch statements to handle exeptions.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    34
    Show some code

    All characters are represented in memory by a numeric assignment.
    For example "F" is represented by 38 in the ASCII character set,
    "f" is represented by 102.

    You may have to declare your variables a char to avoid numeric representation.

    namespace std appears to convert to ASCII with your compiler

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    26
    ok heres the code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    	int x,y;
    	
    	cout<<"Please enter a number: ";
    	
    	cin>>x;
    
    	cout<<"Please enter another number: ";
    
    	cin>>y;
    
    	cout<<"The Final Number is: "<<x * y <<endl;
    
    	return 0;
    
    }

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you were to enter a letter in error, the input read would fail, and the contents of the int variable would be unreliable.

    You might want to check through these to see if any help you.

    A simple fix to your program might be:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int x,y;
      
      cout<<"Please enter a number: ";
      
      if (cin>>x)
      {
        cout<<"Please enter another number: ";
        if (cin>>y)
        {
          cout<<"The Final Number is: "<<x * y <<endl;
        }
      }
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Or:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int x,y;
      
      cout<<"Please enter a number: ";
      
      while(!(cin>>x))
      {
        cin.clear();
        cin.ignore(cin.rdbuf()->in_avail());
        cout << "Not a number. Please enter a number: ";
      }
      cout<<"Please enter another number: ";
      while(!(cin>>y))
      {
        cin.clear();
        cin.ignore(cin.rdbuf()->in_avail());
        cout << "Not a number. Please enter a number: ";
      }
      cout<<"The Final Number is: "<<x * y <<endl;
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Originally posted by bookworm
    Hey Cobra, C++ books are not so fat for nothing.
    True,this is a simple program,but not so simple that u can get away without using FUNCTIONS !!!
    The program is simple because u dont need to embed the functions in classes.
    What is this post about?
    What have functions to do with his problem, especially functions as useless as product()?

    He IS using a function: main.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> He IS using a function: main.

    And he returns from it correctly...!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how would you answer this question?
    By smoking81 in forum Linux Programming
    Replies: 3
    Last Post: 09-08-2008, 03:53 AM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Replies: 4
    Last Post: 10-29-2003, 01:18 PM
  4. Weird string output only for first run
    By Evilelmo in forum C++ Programming
    Replies: 4
    Last Post: 03-04-2003, 09:00 PM
  5. Replies: 22
    Last Post: 11-08-2001, 11:01 PM