Treating Types Of Data (Static Cast?)

This is a discussion on Treating Types Of Data (Static Cast?) within the C++ Programming forums, part of the General Programming Boards category; Hi. I was wondering how you would treat an array, with type char such as if it was year[5], in ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    7

    Treating Types Of Data (Static Cast?)

    Hi. I was wondering how you would treat an array, with type char such as if it was year[5], in which the data that it is entered is treated as categorical data and not quantitative data. I think you use static_cast but I am not sure how to do that. Thanks a bunch!

  2. #2
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Are you asking about associative arrays? If so, check out std::map

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    7
    Well. Maybe, I dont know that seems beyond my knowledge right now.

    Whenever I am inputting a string into an array and the input is numbers (such as years), it seems to skip all the other user prompts for the other variables as if to solve the problem you would need to cin.ignore something.

    But when I make the variable years[5] into just int years, it seems to work. I dont know why.

  4. #4
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    I think I know what you're asking but I'm still not sure.

    If you post some code to illustrate your question I'm sure it could be answered in no time.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,902
    I suppose you hit a roadblock such as:
    Code:
    int x[5];
    cin >> x[0];
    cin >> x[1];
    cin >> x[2];
    cin >> x[3];
    cin >> x[4];
    It would read one, maybe two, and skip the rest.
    This is due to how cin >> works. It's evil implementation leaves newlines that cannot be converted to an integral value in the input buffer.
    On the next line, cin >> will sense something in the buffer and try to read it, but fails, and follows like that.
    The solution is to use cin.ignore() or read into a string with std::getline and the convert to an integral value using stringstreams.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Of course, there's no problem with what Elysia posted -- when trying to read integers and floats and the like, a new-line at the start of the input is no problem whatsoever. It's only when you try to read in a single character that you get a problem, because C++ thinks that a new-line character is a character (because, well, it is). So if you actually have to read in character-at-a-time, then you have to be more careful. (Note that the example you gave doesn't apply: you should have std::string year, and then cin >> year would do exactly what you want.)

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,902
    I'm a bit rusty on exactly when it leaves stuff in the input buffer, but at least it points out that it happens sometimes and can be problematic. Using std::getline will get rid of that problem entirely.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    7
    Ahh yes where's my manners. Code, yes:

    Code:
    #include <iostream>
    #include <fstream>
    #include <conio.h>
    using namespace std;
    
    char model[20];
    char make[20];
    
    int get_int(int default_value){
    	char s[81];
    	cin.getline(s, 80);
    	if (strlen(s) == 0)
    		return default_value;
    	return atoi(s);
    }
    int main()
    {
    	char filename[81];
    	int mileage;
    	int recsize = sizeof(int) + sizeof(model) + sizeof(make) + sizeof(int);
    	int recnumber;
    	int year;
    
    	//Filename
    	cout << "Enter a filename: ";
    	cin.getline(filename, 80);
    
    	//Open File for binary read and write
    	fstream fbin(filename, ios::binary | ios::in | ios::out);
    	if (!fbin) {
    		cout << "Error. File Could Not Be Opened!" << endl;
    		return -1;
    	}
    
    	//Record Number
    	cout << "Enter File Record Number: ";
    	cin >> recnumber;
    	cin.ignore(100, '\n');
    	//Record User Input.
    	
    	cout << "What is the model? ";
    	cin.getline(model, 19);
    
    	cout << "What is the make? ";
    	cin.getline(make, 19);
    
    	cout << "What is the year? ";
    	year = get_int(0);
    
    	cout << "What is the mileage? ";
    	mileage = get_int(0);
    
    	fbin.seekp(recnumber * recsize);
    	fbin.write(model, 20);
    	fbin.write(make, 20);
    	fbin.write(reinterpret_cast<char*>(&year), sizeof(int));
    	fbin.write(reinterpret_cast<char*>(&mileage), sizeof(int));
    	fbin.close();
    	_getch();
    
    }
    I've had to replace those in Red With

    Code:
    char Year[5]
    ...
    cin.getline(Year, 4)
    ^^If I used those instead of the red bolded things on the code. My Following Output Would Occur.

    Code:
    Enter A Filename: (I inserted File Name, Not Important)
    Enter File Record Number 2:
    What is the model? 2600GTS (Making It Up)
    What is the make? Ford
    What is the year? 1900
    What is the mileage? (AFTER I PRESS A NUMBER, PROGRAM CLOSES)
    I think this is due to when inputting 1900, it went to mileage aswell. I think this is like a case of user inputting a string, and then pressing enter, then a user should input another string for another variable, but it assumed enter was it. That's why cin.ignore was used for that. However it is different for here.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,902
    You're using cin.getline, which is bad. You must stop thinking C. There's a much better way in C++.
    And it's called... std::getline. It takes an std::string and the input stream (cin). Then you don't have to worry about buffer overruns or any leftover characters in the input stream.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    7
    HOLD UP. I FOUND OUT WHY.

    My array for Year for some reason needed to be size 6. (Maybe enter was included THEN the Null).

    Edit: Well I said this before your post. I am only still learning from the "C++ Without Fear" Book, so whatever. Thanks!!!

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,902
    Stuck characters in the input buffer. Again, avoid by using std::getline. Don't use cin.getline and avoid char like the plague.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 02:22 PM
  3. Abstract data types in a queue
    By sa125 in forum C Programming
    Replies: 5
    Last Post: 07-27-2008, 03:51 PM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 01:22 PM

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