Program compiles but does not show age data

Question:
I’m anxious to know why my program is notdisplaying the data, yet it compiles perfectly.

I want to read this struct; in particular: age. Ihave two sets of code that I have tried. Neitheris reading the file properly ? they give megarbage,yet they both compile.

My file for age looks something like this:

45 12 14 

Answer:
In C/C++, a program compiling has nothing to dowith whether or not it may run safely orcorrectly.This is true of most compiled languages, but Cand C++ do not try to interfere with the user’sintentions, hence no attempts are made to check validitiesof your program at run time.

That’s your main problem ? the file you createdfor age is a text file and you are trying toread in till the sizeof(int). This will not work because thedata in your file do not correspond to the sizeof int.

The code with the char array works because thesize of char is the same in binary as it is in ASCII.In other words, a char string has the exact samerepresentation for both binary and ASCIIrepresentations.

Here is a simple example of how you can getaround your problem:

**/#include #include struct team{char name[15];int age;};int main(void){        team Teams;        ifstream fout(“age.dat”);        while(fout >> Teams.age)                cout << Teams.age << endl;        return 0;}

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS