Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8668573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:14:50+00:00 2026-06-12T18:14:50+00:00

The program runs smoothly and I have no errors or warnings when its compiled

  • 0

The program runs smoothly and I have no errors or warnings when its compiled its just when it gets the end result I just get a load of random letters and numbers no matter what I put in.

Here is the code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
     int hold;
     int n;
     int * result = new int;
     int * price = new int;
     std::string items[6];

        for (n=0; n<6; n++)
        {
            cout << "Item#" << n+1 << ": ";
            cin >> items[n];
        }
        cout <<  "\nYou Entered: ";
        for (int n=0; n<6; n++)
            cout << items[n] << ", ";

    for (n=0; n<6; n++)
    {
        if (items[n] == "ab"){
        price[n] = 2650;
        }

        else if (items[n] == "ae"){
        price[n] = 1925;
        }

        else if (items[n] == "ie"){
        price[n] = 3850;
        }

        else if (items[n] == "bt"){
        price[n] = 3000;
        }

        else if (items[n] == "pd"){
        price[n] = 2850;
        }

        else if (items[n] == "ga"){
        price[n] = 2600;
        }

    }

    for (n=0; n<6; n++)
    {
     result += price[n];
    }

    cout << "\nTotal gold for this build: " << result;
    cin >> hold;
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:14:52+00:00Added an answer on June 12, 2026 at 6:14 pm

    Well, result is an int *. This kind of variable usually stores the address of another integer variable, which you get with new int in this specific case. However, with

     result += price[n];
    

    you’ll modify that address, which would lead to segmentation faults if you were to actually write/read from *result. This is also the reason why you output is strange:

    cout << "\nTotal gold for this build: " << result;
    

    This prints the adress stored in result, not the value. Make result an integer and it should work.

    Please note that price should be changed too, see Luchian’s answer.

    Exercise

    1. Change your code so that there is no use of new.
    2. Your program could still fail. What is the initial value of result?
    3. What happens if the user provides a code which is not in your list?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a program to process packets. The program runs well if its
You could have a try. The program below compiles and runs smoothly. The address
Program runs once and it both throws data to the pipe and gets it
This program runs just fine, but for some reason when I ask for an
This is my code below. The program runs fine. I don't get any exceptions.
i have a MyObject. when my program runs, i create a new MyObject self.myObject
I have a class called ModelView which inherits from NSOpenGLView. When my program runs
I have a program which runs an external, command line utility and reads the
I have a program that runs in a few threads. The main thread shares
My program runs fine in Netbeans, but I get the following error when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.