Simple C++ Won't Work...

This is a discussion on Simple C++ Won't Work... within the C++ Programming forums, part of the General Programming Boards category; Greetings, I've just started learning C++ today (my friend suggested it) and I've made my first program. Unfortunately, it won't ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    11

    Simple C++ Won't Work...

    Greetings,

    I've just started learning C++ today (my friend suggested it) and I've made my first program. Unfortunately, it won't even compile... This may be due to the fact that I have a Mac, and that the header file conio.h was missing from my system so I had to download one from the Internet. The compiler I use (Xcode) lists 16 errors; 15 in the file conio.h and 1 in my main file. The code is as follows:

    Code:
    #include <iostream>
    #include <conio.h>
    #include <string.h>
    
    using namespace std;
    
    int main () {
        char key;
        cout << "I'm a simple C++ program.\n\n";
    	key = getch();
    	cout << "To quit the program, press " << key;
    	key = getch();
        return 0;
    }
    The error in this code is the first declaration of "key = getch();". The error displayed is "error: 'getch' was not declared in this scope". This may be due, again, to my possibly faulty conio.h.

    Can anyone aid me in my predicament?

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, don't use non-standard code for one...

    remove the getch()'s and use the standard ways of taking in input... here's how I'd do that:
    Code:
    #include <iostream>
    //those other two are not needed
    
    using namespace std;	//shake this habit eventually
    
    int main()
    {
    	char key;
    	cout<<"I'm a simple C++ program\n\n";
    	cin>>key;	//this is a more standard way of taking in a char
    	cout<<"To quit the program, press [ENTER]\n";	//this didn't do what you thought it did.
    	cin.get();	//use this to wait for a keypress
    	return 0;
    }
    Last edited by major_small; 11-24-2005 at 08:15 PM. Reason: bad syntax highlighter.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    Quote Originally Posted by major_small
    well, don't use non-standard code for one...

    remove the getch()'s and use the standard ways of taking in input... here's how I'd do that:
    Code:
    #include <iostream>
    //those other two are not needed
    
    using namespace std;	//shake this habit eventually
    
    int main()
    {
    	char key;
    	cout<<"I'm a simple C++ program\n\n";
    	cin>>key;	//this is a more standard way of taking in a char
    	cout<<"To quit the program, press [ENTER]\n";	//this didn't do what you thought it did.
    	cin.get();	//use this to wait for a keypress
    	return 0;
    }
    mixing 'cin>>' and 'cin.get[line]()' might cause buffer problems, right?

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    Thanks everyone for your speedy replies. It works like a charm. Here's the code I have now:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int alert();
    
    int main () {
    	cout << "Hey!\nAlert! Alert:\n";
    	cout << "I'm a C++ program! YAY!\n\n";
    	alert();
    	cout << "To quit the program, press [ENTER]\n";
    	cin.get();
    	return 0;
    }
    
    int alert() {
    	string message;
    	message = "This is an initialized array with a maximum capacity of 255 characters. Have a nice day!\n\n";
    	cout << message << endl;
    	return 0;
    }
    I'll be sure to recommend this forum to all of my friends.
    Last edited by jothesmo; 11-25-2005 at 04:51 PM.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Just a question why do you have the
    Code:
     
    int alert() {
    	string message;
    	message = "This is an initialized array with a maximum capacity of 255 characters. Have a nice day!\n\n";
    	cout << message << endl;
    	return 0;
    }
    why not just do this instead

    Code:
    #include <iostream>
    #include <string>
     
    using namespace std;
     
    int main () {
    string message = "This is an initialized array with a maximum capacity of 255 characters. Have a nice day!\n\n";
    	cout << "Hey!\nAlert! Alert:\n";
    	cout << "I'm a C++ program! YAY!\n\n";
    	cout << message << endl;
    	cout << "To quit the program, press [ENTER]\n";
    	cin.get();
    	return 0;
    }
    just a note the alert() just seems like a waste of code considering you dont need the extra block of code for it to work. The one I rewrote will work exactly the same as yours it just is one block of code instead of two
    Last edited by Raigne; 11-25-2005 at 04:04 PM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I'm sure jothesmo's learning about functions.

    BTW, in your code, you don't need the function prototype for alert(), since it no longer exists.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > message = "This is an initialized array with a maximum capacity of 255 characters
    Who said it was 255?
    I know of no such restriction unless it's something specific to your broken compiler.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    It could just be a random string that he took from text in the tutorial. Just a thought I was just asking him why he used excessive code for a simple program. Oops I forgot to delete that prototype. Oh well it is fixed now anyways.
    Last edited by Raigne; 11-25-2005 at 04:04 PM.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    Quote Originally Posted by Salem
    > message = "This is an initialized array with a maximum capacity of 255 characters
    Who said it was 255?
    I know of no such restriction unless it's something specific to your broken compiler.
    My compiler isn't broken, it merely does not have the conio.h file. I'm not sure if Apple thought that it would be needed, since not many people compile C++ on a Mac (to my knowledge). I had originally wrote that text because I had the string set at a limit of 255 chars. Afterwards, I removed this and forgot to edit the code.

    Raigne, thank you for pointing that out. I have been working on the code, and I now have a slightly larger file. Here is an excerpt:

    Code:
    int main () {
    	string message = "This is an initialized array. Have a nice day!\n";
    	cout << "\nGreetings! I am a C++ program written by Jonathan Campbell!\n\n";
    	cout << message << endl;
    	name();
    	quiz_intro();
    	quiz();
    	cout << "To quit the program, press [ENTER]\n";
    	cin.get();
    	cin.get();
    	return 0;
    }
    
    int name() {
    	cout << "What's your name? ";
    	getline (cin, uname);
    	cout << "Hello " << uname << "!\n\n";
    	cout << "How are you today? Please reply with fine or ill. ";
    	loop:
    	cin >> uhealth;
    	if (uhealth == "fine")
    		cout << "That's great! I am also feeling " << uhealth << ".\n\n";
    	else if (uhealth == "ill")
    		cout << "Really? How depressing. You know, I am also feeling " << uhealth << ".\n\n";
    	else {
    		cout << "\aPlease enter fine or ill.\n\n";
    		goto loop;
    	}
    	return 0;
    }
    
    int quiz_intro() {
    	cout << "Hey " << uname << ", do you want to take a little quiz? Please reply with yes or no. ";
    	loop:
    	cin >> uquiz1;
    	if (uquiz1 == "yes")
    		cout << "Great! Let's start.\n\n";
    	else if (uquiz1 == "no")
    		cout << "Too bad. :)\n\n";
    	else {
    		cout << "\aPlease enter yes or no.\n\n";
    		goto loop;
    	}
    	return 0;
    }

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    goto's are evil. please learn loops.

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Yes goto's are evil. I would reccomend using do/while loops, or for... I dont reall see the point in goto other than to mess stuff up. Well anyways if you use do/while be very carful not to intterupt your loop. or make it so that after so far in the program you cant go back. I did that and could figure out how to fix it and had to rewrite the entire code. And rewriting 400 lines of code sux.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 07:42 AM
  2. Simple Winsock Wrapper
    By neandrake in forum Networking/Device Communication
    Replies: 12
    Last Post: 03-22-2004, 09:55 PM
  3. Simple class doesn't work
    By Cris987 in forum C++ Programming
    Replies: 7
    Last Post: 01-08-2004, 10:18 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 10:05 AM

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