PDA

View Full Version : Help!! Writing a program to put input in alphabetical order.


LSUSOBEAST
04-15-2010, 03:04 AM
I need to write a program that asks the user to enter names, and then the program puts them in order.

My mind is completely blank. I just started teaching myself how to program, and I'm severely stuck.

Also, in my C++ 2008...I write a successful program. It debugs fully, and has a successful build. The console window pops up. Lets say it displays "Enter a number." The second I enter a number and press enter, the console window closes and I don't get to see the output. This is very frustrating.

Any help is appreciated!!

Thanks!!

BWiz
04-15-2010, 04:17 AM
In reference to the window closing see: http://codingforums.com/showthread.php?t=193765

You need to be able to solve problems to become a successful programmer. First think what the program will need to do (don't think about the actual algorithm you are going to use the sort the names - just the general steps).

Once you have the general steps, then think about how you will store the names. If you have just started programming, I might suggest using an array. Don't be tempted to use anything from the STL (if you don't know what this is - look it up), you need to learn to do it yourself first.

Once you have that, then think about how you would sort them by hand. See if you can't translate those steps into pseudocode, and from that, into C++.

LSUSOBEAST
04-15-2010, 04:27 AM
I still don't quite understand (in reference to the console window opening prematurely).

BWiz
04-15-2010, 04:32 AM
I still don't quite understand (in reference to the console window opening prematurely).

Opening or closing prematurley?

I assume that it is closing prematurley. It's closing because the program has finished executing. To prevent it, use one of the methods:


1) Use the either getch() (conio.h) or cin.get() to pause operation at the end of your program
2) Use system("pause") (don't remember exactly) at the end of your program
3) Execute your program in a console window [Start>run>cmd>%path%to%file/file.exe] rather than launching through explorer

LSUSOBEAST
04-15-2010, 04:39 AM
Like this?

// This program calculates the amount of service fees the account has for a 1 month cycle.

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
double balance, checks, fee1, fee2, totalfees;

cout << "Enter your account balance: $ ";
cin >> balance;
cout << "Enter the amount of checks you have written: ";
cin >> checks;

if (balance < 0)
cout << "Urgent message!! Your account has been overdrawn!!";

if (checks < 20)
fee1 = checks * .10;
if (checks >= 20 && checks <= 39)
fee1 = checks * .08;
if (checks >= 40 && checks <= 59)
fee1 = checks * .06;
if (checks >= 60)
fee1 = checks * .04;

if (balance < 400)
fee2 = 15;
else
fee2 = 0;

totalfees = fee1 + fee2;

cout << "The total fees for the month are: $" << totalfees;

cin.get();

return 0;

}

BWiz
04-15-2010, 04:41 AM
Yes. Personally I would just run the program in a console window.

LSUSOBEAST
04-15-2010, 04:42 AM
The console window still closed prematurely.

BWiz
04-15-2010, 04:49 AM
Use:


cin.ignore();
cin.get();

LSUSOBEAST
04-15-2010, 04:55 AM
Thank you so much!!

That worked like a charm!!!!