Solved

Program runs as if there is no getch() command!

Posted on 1997-05-30
5
650 Views
Last Modified: 2012-05-04

Hello,
         Im attempting to learn C++ using Borland's C++ Builder and a good
 tutorial book which came with it.  I however have run into a problem.
 Using the code supplied with the tutorial the program will give off no
 errors but when run will go on and off in about 1/2 sec.  It is as if
 there is no getch() statement.  Could you PLZ PLZ PLZ look at it and let
 me know what I am doing wrong.... Im about ready to pull my hair out!!
 Thanks
 Aaron Gorsky

 #include <iostream.h>
 #include <conio.h>
 #include <stdlib.h>
 #pragma hdrstop

 #include "structur.h"

 void displayRecord(int, mailingListRecord mlRec);

 int main(int, char**)
 {
   //
   // create an array of mailingListRecord structures
   //
   mailingListRecord listArray[3];
   cout << endl;
   int index = 0;
   //
   // get three records
   //
   do {
     cout << "First Name: ";
     cin.getline(listArray[index].firstName,
       sizeof(listArray[index].firstName) - 1);
     cout << "Last Name: ";
     cin.getline(listArray[index].lastName,
       sizeof(listArray[index].lastName) - 1);
     cout << "Address: ";
     cin.getline(listArray[index].address,
       sizeof(listArray[index].address) - 1);
     cout << "City: ";
     cin.getline(listArray[index].city,
       sizeof(listArray[index].city) - 1);
     cout << "State: ";
     cin.getline(listArray[index].state,
       sizeof(listArray[index].state) - 1);
     char buff[10];
     cout << "Zip: ";
     cin.getline(buff, sizeof(buff) - 1);
     listArray[index].zip = atoi(buff);
     index++;
     cout << endl;
   }
   while (index < 3);
   //
   // clear the screen
   //
   clrscr();
   //
   // display the three records
   //
   for (int i=0;i<3;i++) {
     displayRecord(i, listArray[i]);
   }
   //
   // ask the user to choose a record
   //
   cout << "Choose a record: ";
   char rec;
   //
   // be sure only 1, 2, or 3 was selected
   //
   do {
     rec = getch();
     rec -= 49;
   } while (rec < 0 || rec > 2);
   //
   // assign the selected record to a temporary variable
   //
   mailingListRecord temp = listArray[rec];
   clrscr();
   cout << endl;
   //
   // display the selected record
   //
   displayRecord(rec, temp);
   getch();
   return 0;
 }

 void displayRecord(int num, mailingListRecord mlRec)
 {
   cout << "Record " << num + 1 << ":" << endl;
   cout << "Name:     " << mlRec.firstName << " ";
   cout << mlRec.lastName;
   cout << endl;
   cout << "Address:  " << mlRec.address;
   cout << endl << "          ";
   cout << mlRec.city << ", ";
   cout << mlRec.state << "  ";
   cout << mlRec.zip;
   cout << endl << endl;
 }

 code for Struct.h is as follows

 #ifndef _STRUCTUR_H
 #define _STRUCTUR.H
 struct mailingListRecord {
   char firstName[20];
   char lastName[20];
   char address[50];
   char city[20];
   char state[5];
   int zip;
 };
 #endif
0
Comment
Question by:agorsky
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 3

Expert Comment

by:byang
ID: 1163761
getch() requires you the press the enter key after typing in something. For example, if you entered "12" and then pressed enter, the first call to getch() will return you '1', the next call will return you '2'.

0
 

Author Comment

by:agorsky
ID: 1163762
Ok... But, what do I need to actually add to the code to make it work.  I typed it word for word in the tutorial with no luck. IfI need to type more getch() statements then where would I putthem?Thanks
0
 
LVL 1

Expert Comment

by:rprice032897
ID: 1163763
Tutorial code doesn't always work.  Sad but true.  Try using cin (the counterpart of cout) to grab the incoming string.  You could also look at using a string class with a readstring method.  Getch() and getche() are single character functions.

My other reccommnedation is to learn C++ using the IDE to write the programs and then compile on the command line.  The C++ builder hides alot of the learning from the user and should be used AFTER you understand how the language works.
0
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 100 total points
ID: 1163764
Although the code doesn't look so nice to me, it ran okay when I tested it as a *** console *** application.
Each executable you are creating is espacially targeted at a specific platform, such as Dos, Unix or Windows. The code you wrote is clearly intended to run under Dos or a Win95 console (which you might view 32 bit Dos).
When you create a new project with the BC5 IDE, and presumably also with C++ Builder, the target type's default is Win95 GUI! This will compile your code and run it (in .5 sec or so).
Try changing the application's target type to either 16 bit Dos or Win32 Console.
Good Luck,
.luc.

PS. I agree with rprice's comment on learning C++.
0
 

Author Comment

by:agorsky
ID: 1163765
Thanks for the help... You were right! In project options the linker is set to GUI.... I set it to Console and received a different problem however....It now says...[linker error]Unresolved external "_main" referenced from module c0nt.ASM.Any ideas???
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question