Here's a little program i wrote, i'm trying to pass my structure book to function input. The code works fine on Code::Blocks but VS12 is giving an error on compliation " error C4700: uninitialized local variable 's' used ". What i'm doing wrong?

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

struct book{
        char title[20];
        char author[20];
        int price;
};
book input(struct book);
void display(struct book);
book input(book details)
{
    cout << "Book Title : ";
    gets_s(details.title);
    cout << "\nBook Author : ";
    gets_s(details.author);
    cout << "\nBook : Price : ";
    cin >> details.price;
    return details;
}
void display(book details)
{
    cout<<"\n\nBook Details\n\n"; 
    cout << "\nBook Title : ";
    puts(details.title);
    cout << "\nBook Author : ";
    puts(details.author);
    cout << "\nBook : Price : ";
    cout<<details.price<<endl;
}
int main()
{
    book s;// Here's "s" initialized struct variable
    s = input(s);
    display(s);
    system("PAUSE");
    return 0;
}

because on line 35 you are passing s and s has not been initialized. I would change input() to accept the parameter by reference and not by value.

Why are you using gets()??? you should be using cin.getline()

void input(book& details)
{
    cout << "Book Title : ";
    cin.getline(details.title, sizeof(details.title));
    cout << "\nBook Author : ";
    cin.getline(details.author, sizeof(details.author));
    cout << "\nBook : Price : ";
    cin >> details.price;
}

then line 35 becomes

input(s);

Another way to do it is for input() to have no parameters at all.

book input(void)
{
    book details;
    cout << "Book Title : ";
    cin.getline(details.title, sizeof(details.title));
    cout << "\nBook Author : ";
    cin.getline(details.author, sizeof(details.author));
    cout << "\nBook : Price : ";
    cin >> details.price;
    return details;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.