string trouble

This is a discussion on string trouble within the C++ Programming forums, part of the General Programming Boards category; So i decided my first program is going to be a text game. I have the basic part done, i ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    11

    string trouble

    So i decided my first program is going to be a text game. I have the basic part done, i just can't get the string working. What i want it to do, is
    1) user inputs their name
    2) name is stored under the "name" string
    3) name is then printed to the screen

    I don't really know what i'm doing with strings, but i compared my strings to the dogsim game someone posted on the games forum and they looked the same.

    Code:
    #include <iostream>
    #include <windows.h>
    #include <stdlib.h>
    #include <string>
    
    using std::cin;
    using std::cout;
    using std::string;
    
    unsigned long int money;
    unsigned short int reputation;
    unsigned short int time_worked;
    unsigned short int skill;
    string name;
    string hospital name;
    string state;
    string country;
    
    void start();
    void instruction();
    void begin();
    void yourstats();
    void gamemenu();
    void work();
    void buyitem();
    void sellitem();
    void patientdied();
    void patientgotwell();
    void paycheck();
    void bills();
    void fired();
    void hired();
    void sued();
    void gameover();
    
    void start()
    {
    	cout<<"Welcome to Doctor Sim v1!"\n\n;
    	cout<<"Main Menu"\n;
    	cout<<"1)Begin Simulation"\n;
    	cout<<"2)Instruction"\n";
    	cout<<"3)Exit"\n;
    	cin>>mainmenuchoice;
    	cin.ignore();
    
    	if (mainmenuchoice == "1") {
    		begin();
    	}
    	else if (mainmenuchoice == "2") {
    		instruction();
    	}
    	else if (mainmenuchoice == "3") {
    
    	}
    	else {
    		cout<<"Wrong Answer! Simulation will now begin!"\n\n;
    		begin();
    	}
    
    void instruction()
    {
    	cout<<"What is there to explain? Follow the promts and get money! boink"\n;
    	start();
    }
    void begin()
    {
    	string name;
    	name = "Doctor";
    	cout<<"Hello, are you here to apply for the job?";
    	cin>>answer1;
    	cin.ignore();
    	if (answer1 == "yes") {
    		cout<<"good";
    	}
    	else if (answer1 == "no") {
    		cout<<"That's too bad, you look like a fine doctor. I think you should see the hospital manager anyway.";
    	}
    	cout<<"Hello, you must be the doctor I heard about. We're looking for a another doctor to hire, so let's preceed to the interview.";
    	cout<<"First, what's your name?";
    		cin>>name;
    	cout<<"Let me write this down...Name:" << name << ", what college did you goto?";
    	cout<<"Really...";
    	cin>>;
    	cout<<"Well, i'll hire you, but only on a trial basis. You screw up and you're gone.";
    }
    
    int main()
    {
    	start();
    }
    My output is:

    Code:
    Compiling...
    main.cpp
    D:\programming\code\my code\learning project\main.cpp(15) : error C2146: syntax error : missing ';' before identifier 'name'
    D:\programming\code\my code\learning project\main.cpp(15) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.
    
    main.exe - 2 error(s), 0 warning(s)
    These errors seem simple, but everything seems right to me. I don't understand why it's only the name string that i get an error on. And i'm not too sure on how to end a program (error 2). I just thought i needed a bracket and that was it.

    Any help would be appreciated. Thanks.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    it looks good.. you could just use

    Code:
    using namespace std;
    instead of using all the std:: 's

    also.. I don't really see a need for declaring long and short variables at this stage in the game..

    all you really need to do is just have int main( ) return 0; (since main should always return a value)

    Code:
    int main()
    {
    	start();
    
            return 0;
    }
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    11
    thanks for your suggestions, but i'm still stuck with my errors. I'd like to get this working now, so when i'm working with a nearly complete program, i don't find out i have 100 errors

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>string hospital name;

    Might want to change that line...

    And actually you don't need return 0; at the end of main: it returns 0 implicitly if you don't specify.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    having 100 errors might not be as big of a problem as you think.. sometimes having a 100 errors is easier to solve than just having 1 or 2 errors you just can't figure out..

    I think you forgot a curly brace } after your start( ) function definition.

    if you want, you can take out include<windows.h>

    you can also //comment out all the function prototypes that you won't be using right away
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    11
    deleting the line "string hospital name;", gets me 32 errors

    adding the bracket in start() did nothing.

    with the bracket in start (), my output is:

    Code:
    Compiling...
    main.cpp
    D:\programming\code\my code\learning project\main.cpp(14) : error C2146: syntax error : missing ';' before identifier 'name'
    D:\programming\code\my code\learning project\main.cpp(14) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.
    
    main.exe - 2 error(s), 0 warning(s)
    I tried switching the build configs from debug to release...no change.

    The only thing i can see that would cause an issue, would be the cin.ignore i use. I have no idea what it does, but in the sample code (dogsim game) i use to base mine off of, that's what he used. I use cin.ignore before in the code, but when i input the string i don't use cin.ignore.

    on a side note, what does cin.ignore do? Seems like a class or function...

    Thanks for your responses, i'll keep poking around in the code, and might even rewrite it. Seems like i'm using too much that i don't understand. yet...

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    void start()
    {
    	cout<<"Welcome to Doctor Sim v1!"\n\n;
    	cout<<"Main Menu"\n;
    	cout<<"1)Begin Simulation"\n;
    	cout<<"2)Instruction"\n";
    	cout<<"3)Exit"\n;
    	cin>>mainmenuchoice;
    	cin.ignore();
    
    	if (mainmenuchoice == "1") {
    		begin();
    	}
    	else if (mainmenuchoice == "2") {
    		instruction();
    	}
    	else if (mainmenuchoice == "3") {
    
    	}
    	else {
    		cout<<"Wrong Answer! Simulation will now begin!"\n\n;
    		begin();
    	}
    
    } //   <-------- HERE    fatal error C1004: unexpected end of file found



    also.. what is this..?!???

    excerpt from begin( )
    Code:
    	
    	cin>>;
    Last edited by The Brain; 12-27-2004 at 06:58 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #8
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Well this is what I see right of the bat:

    Code:
        cout<<"Welcome to Doctor Sim v1!"\n\n;
        cout<<"Main Menu"\n;
        cout<<"1)Begin Simulation"\n;
        cout<<"2)Instruction"\n";
        cout<<"3)Exit"\n;
    
    //should be
    
       cout<<"Welcome to Doctor Sim v1!\n\n"; 
       cout<<"Main Menu\n";
       cout<<"1)Begin Simulation\n";
       cout<<"2)Instruction\n";
       cout<<"3)Exit\n;"
    And then of course you forgot the closing bracket } for your start function.

    Happy Coding!

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by howdytest
    deleting the line "string hospital name;", gets me 32 errors
    That's because the error you are getting is causing all the other errors to not show up. Notice that the compile errors you posted refer to that line, so fixing that line might be a good idea. Remove it completely, or change it to "string hospital;". Then, try to understand why.

    A single change won't remove all compile errors. The point of people pointing out certain individual problems is for you to understand why they are problems and fix them so they are correct. The point is not to give you magic potion that makes your erros go away without you understanding why. The point is also not to simply make the errors go away. The errors are there to tell you that your code is incorrect. The goal, then, is to make your code correct, not remove the errors. Once the code is correct, the errors will generally disappear. Hopefully that makes a little sense.

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    11
    excerpt from begin( )

    Code:
    	
    	cin>>;
    That's just an input so the user has to enter something. For the question i posed, i didn't need to save the answer as anything. Just to give the game a realisitic feeling, instead of asking a question then moving on to another without waiting for an answer.

    I've added the bracket to the end of start(), still getting the error "unexpecting end of file".

    Andy, i switched out what you suggested, still getting both errors.

    That's because the error you are getting is causing all the other errors to not show up. Notice that the compile errors you posted refer to that line, so fixing that line might be a good idea. Remove it completely, or change it to "string hospital;". Then, try to understand why.

    A single change won't remove all compile errors. The point of people pointing out certain individual problems is for you to understand why they are problems and fix them so they are correct. The point is not to give you magic potion that makes your erros go away without you understanding why. The point is also not to simply make the errors go away. The errors are there to tell you that your code is incorrect. The goal, then, is to make your code correct, not remove the errors. Once the code is correct, the errors will generally disappear. Hopefully that makes a little sense.
    I understand what you're trying to say. And just so you know, i'm not asking for a free solution. This is a learning project. I'm posting here trying understand what's going on. The reason i started this project to feel comfortable writing code, not to just write some program. One day, i'll be able to look at my code and realize i did something wrong. One day i'll be able to read the error messages and understand my mistake. But i'm not quite there yet. And at the moment, this forum is my only way of asking questions.

    I'm not one to give up on something, i've probably looked over this little segment of code at least 50 times looking for a problem. I just don't have the understanding to solve it.

    This programming stuff is harder than i thought. At first i was reluctant to start making the files, but once i got started it was easy! It just got alot harder...
    Last edited by howdytest; 12-27-2004 at 08:09 PM.

  11. #11
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    It seems that a quotation is still out of place. For instance:
    Code:
    cout<<"3)Exit\n;"
    Is incorrect syntax. Rather:
    Code:
    cout<<"3)Exit\n";
    Is correct syntax.

    These errors can be catchy. You may want to keep some references on C++ syntax. Here are a few that may help in the future:

    The C++ Syntax Guide is a good reference in this case if you look under the Basic I/O subsection.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  12. #12
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    you can't do this:

    Code:
    string hospital name;
    However you can do this:
    Code:
    string hospital_name;
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  13. #13
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by howdytest
    I understand what you're trying to say. And just so you know, i'm not asking for a free solution. This is a learning project. I'm posting here trying understand what's going on. The reason i started this project to feel comfortable writing code, not to just write some program. One day, i'll be able to look at my code and realize i did something wrong. One day i'll be able to read the error messages and understand my mistake. But i'm not quite there yet. And at the moment, this forum is my only way of asking questions.
    Don't worry, you are doing fine. Do you understand why "string hospital name;" is incorrect? Do you understand why "cout<<"3)Exit\n;"" is incorrect? How about the missing bracket? My point is simply that for each error pointed out by someone here, see if you can understand why your code is wrong, and that way you will understand if your fix is correct regardless of whether more or less compiler errors appear.

    In the future, always compile as you go. You wrote entirely too much code before trying to compile it. Especially as a beginner, new code should be compiled every few lines.

  14. #14
    Registered User
    Join Date
    Oct 2004
    Posts
    11
    Thanks everyone. Must seem like you're walking me through this

    So far, i've narrowed it down from 32 errors, to 19.

    Thanks for all your help.

  15. #15
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    There were a lot of little small syntax errors throughout your code. I am going to list your code sample with all the fixes. I strongly suggest you compare it to yours line by line to see what the problems were.

    Code:
    #include <iostream>
    #include <windows.h>
    #include <stdlib.h>
    #include <string>
    
    using std::cin;
    using std::cout;
    using std::string;
    
    unsigned long int money;
    unsigned short int reputation;
    unsigned short int time_worked;
    unsigned short int skill;
    string name;
    string hospital_name;
    string state;
    string country;
    
    void start();
    void instruction();
    void begin();
    void yourstats();
    void gamemenu();
    void work();
    void buyitem();
    void sellitem();
    void patientdied();
    void patientgotwell();
    void paycheck();
    void bills();
    void fired();
    void hired();
    void sued();
    void gameover();
    
    void start()
    {
    	int mainmenuchoice;
    
    	cout<<"Welcome to Doctor Sim v1!\n\n";
    	cout<<"Main Menu\n";
    	cout<<"1)Begin Simulation\n";
    	cout<<"2)Instruction\n";
    	cout<<"3)Exit\n";
    	cin>>mainmenuchoice;
    	cin.ignore();
    
    	if (mainmenuchoice == 1) {
    		begin();
    	}
    	else if (mainmenuchoice == 2) {
    		instruction();
    	}
    	else if (mainmenuchoice == 3) {
    	}
    	else {
    		cout<<"Wrong Answer! Simulation will now begin!\n\n";
    		begin();
    	}
    }
    void instruction()
    {
    	cout<<"What is there to explain? Follow the promts and get money! boink\n";
    	start();
    }
    void begin()
    {
    	string name, answer1;
    	
    	name = "Doctor";
    	cout<<"Hello, are you here to apply for the job?";
    	cin>>answer1;
    	cin.ignore();
    	if (answer1 == "yes") {
    		cout<<"good";
    	}
    	else if (answer1 == "no") {
    		cout<<"That's too bad, you look like a fine doctor. I think you should see the hospital manager anyway.";
    	}
    	cout<<"Hello, you must be the doctor I heard about. We're looking for a another doctor to hire, so let's preceed to the interview.";
    	cout<<"First, what's your name?";
    		cin>>name;
    	cout<<"Let me write this down...Name:" << name << ", what college did you goto?";
    	cout<<"Really...";
    	cin.get();
    	cout<<"Well, i'll hire you, but only on a trial basis. You screw up and you're gone.";
    }
    
    int main()
    {
    	start();
    }
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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