Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,488 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,701 people online right now. Registration is fast and FREE... Join Now!




Help with getting names from file to show on console

 
Reply to this topicStart new topic

Help with getting names from file to show on console

rttlsnake
6 Mar, 2008 - 10:30 AM
Post #1

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 10

I am ahving trouble getting the names in a text file to display on the console. The text file is setup like this:

Bob Julie Amy Bubba Thor

here is my code, any help will be greatly appreciated.

CODE

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int play_or_quit(int num);
const int SizeOfString = 7;
void CnameInputFile(string cname[SizeOfString]);
void CnameOutputFile(string cname[SizeOfString]);
void stars();

int main()
{
    int num;
    int pnum, cnum;
    char pname[25];
    bool gameover;
    string cname[SizeOfString];

    cnum = (rand() + time(0)) % 100;
    gameover = false;
    
    stars();
    cout << "***********| Welcome to |***********" << endl;
    cout << "***********|    High    |***********" << endl;
    cout << "***********|   Number   |***********" << endl;
    stars();
    
    cout << endl;
    cout << "    Continue = 1    Quit = 2        \n";
    cin >> num;
    cout << endl;

    if (num != 1)
        return 1;

    cout << endl;
    cout << "    Continue = 1    Quit = 2        \n";
    cin >> num;
    cout << endl;

    if (num != 1)
        return 1;

    cout << "Player please enter your name:\n";
    cin >> pname;
    cout << endl;

    CnameInputFile(cname);
    CnameOutputFile(cname);

    cin >> pname;

    cout << "          " << pname << " vs. The Computer" << endl;
    cout << endl;

    while (!gameover)
    {
        cout << pname << " pick a number between 1 and 100\n";
        cin >> pnum;
        cout <<endl;
        cout << "The number you chose is " << pnum << endl;
        cout << endl;
        cout << "The computer is picking its number\n";
        cout << cnum;
        cout << endl;
        
        if (pnum > cnum)
        {
            cout << pname << " is the winner" << endl;
            gameover = true;

            return 0;
        }

        else
            if (pnum < cnum)
                cout << "Computer is the winner " << endl;
                cout << "Try again!" << endl;
                cout << endl;
    }

    return 0;
}

void stars()
{
    cout << "************************************" << endl;
    cout << "************************************" << endl;
    cout << "************************************" << endl;
}

void CnameInputFile(string cname[SizeOfString])
{
    ifstream incnames;
    incnames.open("ComNames.txt");
    int i;

    for(i = 0; i < SizeOfString - 1; i++)
    {
        incnames >> cname[i];
    }
    
    incnames.close();
    
}

void CnameOutputFile(string cname[SizeOfString])
{
    int j = 0;

    if(cname[j], j++)
        cout << cname[j];

}


User is offlineProfile CardPM
+Quote Post

MorphiusFaydal
RE: Help With Getting Names From File To Show On Console
6 Mar, 2008 - 10:38 AM
Post #2

D.I.C Lover
Group Icon

Joined: 12 May, 2005
Posts: 1,105



Thanked: 8 times
Expert In: Hardware, Networking

My Contributions
Can you describe what's happening currently, and what's supposed to be happening?
User is offlineProfile CardPM
+Quote Post

rttlsnake
RE: Help With Getting Names From File To Show On Console
6 Mar, 2008 - 11:01 AM
Post #3

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 10

QUOTE(MorphiusFaydal @ 6 Mar, 2008 - 11:38 AM) *

Can you describe what's happening currently, and what's supposed to be happening?



It is not reading the words in or it is but it won't display them on the console, I am wanting it to display the 5 names to the user

thnx!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 07:32PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month