0

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;
}

Edited by waleedahmad

2
Contributors
1
Reply
13
Views
3 Years
Discussion Span
Last Post by Ancient Dragon
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;
}

Edited by Ancient Dragon

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.