cin.get(); doesn't work even with cin.ignore();

This is a discussion on cin.get(); doesn't work even with cin.ignore(); within the C++ Programming forums, part of the General Programming Boards category; I have another question but I don't want to make a new topic for it. When defining variables you can ...

  1. #31
    Code Master calumn's Avatar
    Join Date
    May 2006
    Location
    Scotland
    Posts
    16
    I have another question but I don't want to make a new topic for it.
    When defining variables you can use int for integers and string for "text"
    The string variable can hold text (ie letters) and numbers but int can only hold numbers
    Why would you want to use int.
    Say i was asking for an age one person might type it in as a number like 12 but someone else might type in the actual word like twelve
    Why would you want to use int instaed of string
    Does string have any limitations in the program or is it just bad programing.
    I know VB 6 and .NET
    I also know actionscripting in Flash and I am OK in python if that counts and was learning pascal but gave up because I could find a good compiler. I am now learning C++ and I think its reall cool.

  2. #32
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    Well, programmers like to use data types responsibly. This kind of issue comes up because you always get input from the user in string format, and in order to have a real -number- a conversion must take place.

    There are times when leaving numbers in string format is easier or best: like dates, or IP addresses, but for 90% of the time you want to store a number in a number type because:
    - doing math is easy
    - the number is represented in a compact format
    - it's easier to check to see if the numerical data is an error itself, like your age program showed

  3. #33
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    One more thing - strings can handle numbers as well. For
    example, you can declare a string variable and use it for a
    numerical selection (1, 2, 398, 10 etc). The thing is, the string
    data type handles characters - which themselves are a data
    type (look up char somewhere). A string is merely a group of
    characters.

    The problem is, that if you used a string (or a single char), the
    numbers you enter are in a completely different format than
    if the data type was an integer. This is due to ASCII, the standard
    encoding for characters. Characters are typically represented
    internally by 1 byte (allowing for 0-255 unique values). The
    problem lies with the fact that the equivalent ASCII "code" for
    the number 0 doesn't start at 0 - it is 48 (obviously not the
    direct binary equivalent).

    This can cause all sorts of problems - such as performing math
    operations, or even representing numbers with 2 digits or more.
    So we have integers - which represent the numbers we enter in
    their binary equivalent. We can do math operations and represent
    numbers bigger than 9 using their direct binary equivalent.

    There are also differences in performing comparisons,
    assignments/initialisations, and so on. Take a look at this
    brief example and not the highlighted difference. Run it and
    see a huge difference in the output for the same number.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main (void)
    {
    	int a_num = 9;
    	char a_char = '9';
    	string a_string = "9";
    
    	cout << a_num + a_num << endl;		//performing normal addition
    	cout << a_char + a_char << endl;	//outputs sum of their decimal equivalent codes
    	cout << a_string + a_string << endl;	//Results in appending one string onto another
    
    	cin.get ();
    
    	return 0;
    }
    Lastly, characters, integers and strings all have different sizes in
    memory - as I said, a char is typically 1 byte, and string is actually
    a class - which means that there is a lot more than meets the
    eye - much larger than a char or an int.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #34
    Code Master calumn's Avatar
    Join Date
    May 2006
    Location
    Scotland
    Posts
    16
    Wow there is a huge difference in the numbers that it returned.
    the int was right and said 18, I don't have a clue wat char did and string just added them together in a line instead of adding them
    I know VB 6 and .NET
    I also know actionscripting in Flash and I am OK in python if that counts and was learning pascal but gave up because I could find a good compiler. I am now learning C++ and I think its reall cool.

  5. #35
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Well like i said, the integer is the best way to represent real
    numbers (well it won't handle decimal fractions, but there's also
    a float type, and a double type which handle them). The ASCII
    value of character 9 is 57. This means the in binary, if the data
    type is char, the 9 looks like 00111001. Adding it to itself
    produces 114 which is actually the character 'r', but the decimal
    equivalent value is outputted. Then with the strings, the + operator
    appends one string onto another, resulting in a string containing
    two characters - 9 and 9 (strings are groups of characters, remember?)
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 03:48 AM
  2. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 10:53 AM
  3. help getting program to work
    By jlmac2001 in forum C Programming
    Replies: 2
    Last Post: 11-13-2002, 10:04 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21