Memory and int not being declared.

This is a discussion on Memory and int not being declared. within the C++ Programming forums, part of the General Programming Boards category; Good afternoon everyone, I had a quick question regarding a program I am trying to complete. I have the basics ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    7

    Memory and int not being declared.

    Good afternoon everyone, I had a quick question regarding a program I am trying to complete. I have the basics worked out, and everything seems to be done, but I am running into two issues.

    1= The program keeps telling me "Run-Time Check Failure #3 - The variable 'order' is being used without being initialized."

    and

    2= When I reach my output screen I receive "Unhandled exception at 0x7751c41f in CISC 192 Project 3.exe: Microsoft C++ exception: std:Out_of_range at memory location 0x002eefb8.."

    While the first one isn't necessarily a deal breaker the second one definitely is. Any help with this would be greatly appreciated.

    Code:
    // Bookstore Project 3.cpp : Defines the entry point for the console application.
    // Declarations
    
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <string>
    #include <istream>
    
    using namespace std;
    
    
    // Start Function Prototypes
    void splash(void);
    void header(string screenLine);
    void menu(string isbn, string title, string author, string new_used, string enrolled, string requirement, int order);
    void input(string isbn, string title, string author, string new_used, string enrolled, string requirement);
    void output(string isbn, string title, string author, string new_used, string enrolled, string requirement, string resp, int order);
    // End Function Prototypes
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
        string isbn;
        string title;
        string author;
        string new_used;
        string enrolled;
        string requirement;
        int order;
    
        // Use User Functions As Defined Below.
        splash ();
        menu(isbn, title, author, new_used, enrolled, requirement, order);
    
        
        return 0;
    }
    
    // Start User Functions
    void splash(void) //Splash Screen To Display On Program Start
    {
        system ("cls");
        cout << endl;
        cout << setw(53) << "San Diego Miramar College" << endl;
        cout << endl;
        cout << setw(52) << "Bookstore Order Program" << endl;
        for (int i=0; i<3; i++)
            cout << endl;
        cout << setw(48) << "Revision #: 1.1" << endl;
        cout << endl;
        cout << setw(52) << "Program By: Joshua Hess" << endl;
        for (int i=0; i<12; i++)
            cout << endl;
        cout << setw(53) << "Press <ENTER> To Continue";
        cin.get();
    }
    
    void header(string screenLine)
    
    {
    
        string line1="San Diego Miramar College";
    
        string line2="Bookstore Order Program";
    
        system("cls");
    
        cout << "                            "<<line1<<endl;
    
        cout << "                            "<<line2<<endl;
    
    
    
    }
    
    void input(string isbn, string title, string author, string new_used, string enrolled, string requirement)//Input Screen For User To Input Information
    {
        string screenName = "Data Input Screen";
        string screenLine= "Main Menu";
    
        system ("cls");
        header(screenLine);
        cout << endl;
        cout << setw(50) << screenName << endl;
        cout << endl;
        cout << endl;
        cout << "Enter The Book ISBN: ";
        getline(cin, isbn);
        cout << "Enter The Book Title: ";
        getline(cin, title);
        cout << "Enter The Book Author: ";
        getline(cin, author);
        cout << endl;
        cout << endl;
        cout << setw(79) << "------------------------------------------------------------------------------" << endl;
        cout << endl;
        cout << endl;
        cout << "Enter New or Used Book (N / U): ";
        getline(cin, new_used);
        cout << "Enter Required or Not Required (R / NR): ";
        getline(cin, requirement);
        cout << "Enter the Estimated Enrollment: ";
        getline(cin, enrolled);
    }
    
    void menu(string isbn, string title, string author, string new_used, string enrolled, string requirement, int order) //Menu Function To Allow User To Select Input, Output, or Quit.
    {
        string option1 = "<I>nput";
        string option2 = "<O>utput";
        string option3 = "<E>xit";
        string prompt = "Enter I, O, or E  ";
        string screenLine = "Main Menu";
        string resp;
            
        do
        {
            system ("cls");
            header(screenLine);
            cout << endl;
            cout << setw(45) << screenLine << endl;
            cout << endl;
            cout << endl;
            cout << setw(30) << option1 << endl;
            cout << endl;
            cout << setw(30) << option2 << endl;
            cout << endl;
            cout << setw(30) << option3 << endl;
            cout << endl;
            cout << setw(32) << prompt; 
            getline(cin, resp);
    
            switch(resp.at(0))
            {
                case 'I':
                case 'i':
                    input(isbn, title, author, new_used, enrolled, requirement);
                    break;
                    
                case 'O':
                case 'o':
                    output(isbn, title, author, new_used, enrolled, requirement, resp, order);
                    break;
    
                case 'E':
                case 'e':
                    break;
    
                default:
                    cout << "Entry Error - Enter I, O, or E: ";
                    getline(cin, resp);
    
            }
        }while((resp.at(0) !='E')&&(resp.at(0) !='e'));
    
    }
    
    void output(string isbn, string title, string author, string new_used, string enrolled, string requirement, string resp, int order) //Results Screen To Display Input And Calculations
    {
        string screenLine = "Main Menu";
        string screenName = "Data Output Screen";
    
        system ("cls");
        header(screenLine);
        cout << endl;
        cout << setw(26) << "ISBN : " << isbn << endl;
        cout << setw(26) << "Title : " << title << endl;
        cout << setw(26) << "Author : " << author << endl;
        cout << endl;
        cout << setw(79) << "------------------------------------------------------------------------------" << endl;
        cout << endl;
        cout << setw(38) << "Projected Number of Books To Order" << endl;
        cout << setw(77) << "For required books: Expected percentage of students buying a new book" << endl;
        cout << setw(43) << "at 47%, and 35% purchasing if used." << endl;
        cout << setw(76) << "For non-required books: Expected percentage of students buying a new" << endl;
        cout << setw(48) << "book at 26%, and 10% purchasing if used." << endl;
        cout << endl;
        cout << setw(29) << "Expected Enrollment: " << enrolled << endl;
        cout << setw(37) << "Book Status (N=New, U=Used): ";
    
        //Will display New and Used instead of N and U
        if ((new_used.at(0)=='N')||(new_used.at(0)=='n'))
            cout << "New" << endl;
        else
            cout << "Used" << endl;
    
        cout << setw(40) << "Required / Not Required (R/NR): ";
    
        //Will display Required and Not Required instead of R and NR
        if ((requirement.at(0)=='R')||(requirement.at(0)=='r'))
            cout << "Required" << endl;
        else
            cout << "Not Required" << endl;
        cout << endl;
    
        //Order Calculations
        if ((new_used.at(0)=='N')||(new_used.at(0)=='n'))
            if ((requirement.at(0)=='R')||(requirement.at(0)=='r'))
            order = (int)(atof(enrolled.data())*.47);
            else
            order = (int)(atof(enrolled.data())*.26);
        
        if ((new_used.at(0)=='U')||(new_used.at(0)=='u'))
            if ((requirement.at(0)=='R')||(requirement.at(0)=='r'))
            order = (int)(atof(enrolled.data())*.35);
            else
            order = (int)(atof(enrolled.data())*.1);
        
        cout << setw(47) << "Books to Order: " << order << endl;
        cout << setw(53) << "Press <Enter> To Continue" << endl;
        getline(cin, resp);
    }
    // End User Functions

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    Never ignore warnings, fix the problem. Many times warnings indicate a serious problem that needs attention.

    I suggest you run this program thru your debugger. The debugger will allow you to locate the exact place where the problem is detected and allow you to view the variables at the time of the crash. You are trying to access one of your strings out of bounds.

    Since you are passing all your variables into your functions by value I suspect this is part of the problem. When you pass variables by value the changes made in the functions are not reflected in the calling function, the changes are lost when the function returns.


    Jim

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,590
    Quote Originally Posted by Joshua Hess View Post
    The program keeps telling me "Run-Time Check Failure #3 - The variable 'order' is being used without being initialized."
    that's because in line 32:

    Quote Originally Posted by Joshua Hess View Post
    Code:
    int order;
    you're declaring order without initializing it. its value is undefined. and then on line 36:

    Quote Originally Posted by Joshua Hess View Post
    Code:
    menu(isbn, title, author, new_used, enrolled, requirement, order);
    you use that variable, still in its uninitialized state.

    C++ does not automatically initialize variables. the only exception is when a variable is static. the compiler will then default-construct the object.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ::wcstold has not been declared.
    By User Name: in forum C++ Programming
    Replies: 5
    Last Post: 08-25-2011, 05:35 PM
  2. Where can & can't variables be declared in C?
    By cpjust in forum C Programming
    Replies: 5
    Last Post: 11-17-2007, 06:42 PM
  3. Replies: 3
    Last Post: 10-12-2006, 07:58 AM
  4. Check if is declared
    By maxorator in forum C++ Programming
    Replies: 11
    Last Post: 08-26-2005, 10:42 AM
  5. declared row and column
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-16-2001, 12:55 PM

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