C++ Age won't display (struct)

I have written a code using struct and pointers, the problem is that when you enter for age, the result would be zero 0. It would be helpful if you take a look at my code

Person's name: Billy
    Person's age: 25
    Write your hobby[0]:Kungfu 
    Write your hobby[1]:soccer
    write your hobby[2]:basketball
    Write your crush name: Jake
    Name:Billy
    Age:0
    Hobbies[0]:Kungfu
    Hobbies[1]:soccer
    Hobbies[2]:basketball
    Crush: Jake

For some reason Age is assign to 25 but in result it shows 0 why is it happening

#include <iostream>
#include <string>
#include <Windows.h>
#include <sstream>
using namespace std;

//declare structure to store info about Billy
struct Son{
    string name;
    string crush;
    int age;
    string hobbies[3];
}Person;

int main(){
    string sAge;
    int i;
    Son* info = new Son;
    info = &Person;
    //user interface
    //Person's name
    cout << "Person's name: ";
    getline(cin, info ->name); //inputs person's name
    //Person's age
    cout << "Person's age: ";
     //inputs person's age
    getline(cin,sAge);
    (stringstream)sAge << info ->age; 
    //for loop to get hobbies
    for(i = 0; i < 3; i++){
        cout << "Write your hobby[" << i <<"]: ";
        getline(cin,info ->hobbies[i]); //inputs the person hobby three times
    }
    //Person's crush
    cout << "Write your crush name: ";
    getline(cin, info ->crush); //inputs the person's crush *opitional*

    //output statement
    cout << "Name: " << info ->name << endl; //display name
    cout << "Age: " << info ->age << endl; //display age
    for(int j = 0; j < 3; j++){ //display hobbies
    cout << "Hobbies[" << j << "]: " << info ->hobbies[j] << endl;
    }
    cout << "Crush: " << info ->crush << endl; //display crush                                             
    delete info;
    system("pause");
    return 0;
}

Answers


(stringstream)sAge << info ->age;

This is not the correct way of doing it, you can use either one of those:

stringstream ss;
ss << sAge;
ss >> info->age;

info->age = atoi(sAge.c_str());

Alternatively, you can use std::atoi(sAge) which accepts std::string right away, however, since you have using namespace std; it should use it automatically. Thanks to @alexolut for stating this.

You're basically casting string to stringstream which is a huge failure, stringstream isn't a subclass of string. Also you shouldn't use C-style casts in C++ at all, learn to use static_cast, reinterpret_cast and dynamic_cast to generate better warnings (Thanks to @JohnZwinck for stating this).

Side note: you're including Windows.h and you're not using any of it's functions.


Need Your Help

Should a REST API be case sensitive or non case sensitive?

rest asp.net-web-api

At work we got a problem with case sensitive REST api which ignores wrongly spelled parameters without returning any error. In my opinion this is bad. Then it comes the general question:

Data Tables: brake with url

javascript php .htaccess datatables

I have a situation with Data Tables that I don't understand: