• We should now be fully online following an overnight outage. Apologies for any inconvenience, we do not expect there to be any further issues.

Reading first letter of every word in txt file

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Right now I have my main (Menu) function which then calls a function entitled "Card_Array". Card Array generates 3 integers (i, j, k) via random number generators. I then WANT these 3 variables to go from "Card_Array" into "Card Assignments". I keep getting identifier not found when trying to call the "Card_Assignments()" within "Card_Array". Here is a brief part of my code:

//These 2 lines are in the main function. Later in the function I type Card_Array()
void Card_Assignments(int &i,int &j,int &k);
void Card_Array();

void Card_Array()
{
cout << "\n---------------------------------------------";
cout << "\n\nWelcome to 'War'\n\n";

srand((unsigned)time(0));
int i = rand()%(b-a+1) + a;
int j = rand()%(c-a+1) + a;
int k = rand()%(d-b+1) + b;

Card_Assignments(i, j, k);
}

void Card_Assignments(int &i,int &j,int &k)
{
if ((count%2)==0)
if(i==1)
{
...
}

What do I have to do to get all of these values moving around without using global variables.

I have managed to solve this problem, with you guys help. Now however, I am stuck once again. I'll optimize my coding later.

I am stuck on reading the first letter of the every word from a file. I have tried to manipulate the stringVar.find() command but I can't get it to work. I have a txt file that I made up that when you grab the first letter of every word, I then store it to a character array. When completed the array of characters will spell out another word.

Right now I can't figure out how to find the index of the first character-- can someone just give me some general help?

-Kevin
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
They are declared and instantiated in Card_Array as const int. Their purpose is to be the bounds of the random numbers (i must be either a 1 or a 2, j must be an int 1-4, k must be an int 2-14).

-Kevin
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
1) Not having your a,b,c,d variables defined could be an issue.

2) Logically, I do not see a problem with the definition of the functions and the way you are using it.

3) Make sure that you have the proper include header files to support the time and I/O
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
1.) They are all defined and instantiated.
2.) I am getting "Card_Assignments" identifier not found
3.) ctime and iostream are both included. I don't believe I am having problems via header files.

-Kevin
 

stevf

Senior member
Jan 26, 2005
290
0
0
at work so cant examine it in great detail but as common said the function definitions look ok - are they all spelled/capitalized the same? perhaps the case is off in one of the lines
 

Hmongkeysauce

Senior member
Jun 8, 2005
360
0
76
if a,b,c,d are declared in Card_Array(), then either the code you posted above is missing the declarations or you simply forgot to put them in yourself. other than that, everything else looks fine.
 

Cerebus451

Golden Member
Nov 30, 2000
1,425
0
76
From your description, you say those function declarations are within your main function. I picture this as:
int main (...)
{
void Card_Assignments(int &i,int &j,int &k);
void Card_Array();
}

If that is the case, then those function declarations only have scope within function main. As soon as you leave function main, they are no longer in scope, which would cause the call in Card_Array to fail since it does not have a prototype for Card_Assignments available. Typically function prototypes are listed at the top of the source file so that they have scope within the entire file.

If your function prototypes are outside of the actual main function and at the top of the file, you might have to post more to see what is going on.
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Here is the entire main function:

#include <iostream>
#include <iomanip>
using namespace std;

void Instructions();
void Card_Assignments(int &i,int &j,int &k);
void Card_Array();

int main()
{
int Menu_Selection;

cout << setw(45) << "Welcome to the classic card game 'War' \n";
cout << setw(37) << "Press enter to continue \n";
cin.ignore(INT_MAX, '\n');

Start:
cout << "Your options are: \n";
cout << setw(28) << "(1) View Instructions \n";
cout << setw(27) << "(2) View High Scores \n";
cout << setw(27) << "(3) Start a new game \n";
cout << setw(24) << "(4) Exit Program \n\n";
cout << "What would you like to do: ";
while( !( cin >> Menu_Selection ) )
{
cin.clear();
cin.ignore( 100, '\n' );
cout << "Please choose an option ";
}

switch (Menu_Selection)
{
case 1:
Instructions();
cout << "\n-----------------------------------------------------\n\n";
goto Start;
case 2:
cout << "\nNot finished yet \n";
break;
case 3:
cout << "\nBeginning New Game... \n";
Card_Array();
//cout << "You have drawn a ";
Card_Array();
//cout << "Your oponent has drawn a ";
break;
case 4:
cout << "\nYou have chosen to exit the program. Goodbye \n\n";
break;
default:
cout << "\nInvalid response. Please execute one of the options.\n\n";
goto Start;
}

return 0;
}

It then goes to Card_Array:

#include <iostream>
#include <ctime>

using namespace std;

const int a=1, b=2, c=4, d=14;
int count=1;

void Card_Array()
{
cout << "\n---------------------------------------------";
cout << "\n\nWelcome to 'War'\n\n";

srand((unsigned)time(0));
int i = rand()%(b-a+1) + a;
int j = rand()%(c-a+1) + a;
int k = rand()%(d-b+1) + b;

if((count%2)!=0)
{
cout << "You have drawn a ";
count++;
}
else
{
cout << "Your oponent has drawn a ";
count++;
}

Card_Assignments(i, j, k);
}

It then goes to Card_Assignments:

#include <iostream>
using namespace std;

void Card_Assignments(int &i,int &j,int &k)
{
...
}
 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
Originally posted by: Gamingphreek
What do I have to do to get all of these values moving around without using global variables.
Build a C++ class. One of the convenient things about classes is that you can create 'local' global variables.

something like:

class Card
{
int _i, _j, _k;

void reset() { _i = 0; _j = 0; _k = 0; }

void assign(int i, int j, int k) { _i = i; _j = j; _k = k; }

void get(int& i, int& j, int& k) { i = _i; j = _j; k = _k; }
};
 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
Assuming these are three different files:

There is a prototype for Card_Assignments on line 6 in - Here is the entire main function:

but not one in - It then goes to Card_Array:
 

sao123

Lifer
May 27, 2002
12,653
205
106
Originally posted by: seemingly random
Originally posted by: Gamingphreek
What do I have to do to get all of these values moving around without using global variables.
Build a C++ class. One of the convenient things about classes is that you can create 'local' global variables.

something like:

class Card
{
int _i, _j, _k;

void reset() { _i = 0; _j = 0; _k = 0; }

void assign(int i, int j, int k) { _i = i; _j = j; _k = k; }

void get(int& i, int& j, int& k) { i = _i; j = _j; k = _k; }
};

As a former C++ instructor at Penn State, I must say, this is the best solution...
encapsule your entire application into a main class, then all the variables are accessable in all functions without making them global.
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Well what I did was put the files into separate functions on one file. Prior to this I had each function in a separate file. It works now...now there is just a lot of debugging on my end :-\
 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
If you intend to do much c++ programming, classes will inevitably need to be learned. A small, non-trivial app is an excellent way to learn - or if anxious to get the app finished, convert it later. classes are a pita to learn initially but will be appreciated later.
 

aCynic2

Senior member
Apr 28, 2007
710
0
0
I think I might see a problem...bare with me, I might have this wrong as I'm still learning the nuances of C++:


Nope, I was wrong. Looking in my book, it shows it the same as you, BUT, it doesn't have variables in the function declaration. In your case it would look like:

void Card_Assignment ( int &, int &, int & );

They also don't reuse the same variable names. That shouldn't matter due to scope rules, but I tend to not as well (but I also don't use i, j, k for anything other than loop interators).
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Now the light dawns
You had multiple files.
The definitions for Card_Assignment was not within the "scope" of the file that was calling it.

The prototype was defined in the first file which was unknown by the second and third
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Now the light dawns
You had multiple files.
The definitions for Card_Assignment was not within the "scope" of the file that was calling it.

The prototype was defined in the first file which was unknown by the second and third

Yep :) ... I thought as well as separate functions that good programming practices suggested separate files as well. Obviously I was wrong. After combining all the functions into one gigantic file everything works (It working kinda wrong right now, but the program is running :) )

I am stuck on reading the first letter of the every word from a file. I have tried to manipulate the stringVar.find() command but I can't get it to work. I have a txt file that I made up that when you grab the first letter of every word, I then store it to a character array. When completed the array of characters will spell out another word.

Right now I can't figure out how to find the index of the first character-- can someone just give me some general help?

-Kevin
 

aCynic2

Senior member
Apr 28, 2007
710
0
0
Tokenize each line into individual words, then you can read the first character of each.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: Gamingphreek
Now the light dawns
You had multiple files.
The definitions for Card_Assignment was not within the "scope" of the file that was calling it.

The prototype was defined in the first file which was unknown by the second and third

Yep :) ... I thought as well as separate functions that good programming practices suggested separate files as well. Obviously I was wrong. After combining all the functions into one gigantic file everything works (It working kinda wrong right now, but the program is running :) )

I am stuck on reading the first letter of the every word from a file. I have tried to manipulate the stringVar.find() command but I can't get it to work. I have a txt file that I made up that when you grab the first letter of every word, I then store it to a character array. When completed the array of characters will spell out another word.

Right now I can't figure out how to find the index of the first character-- can someone just give me some general help?

-Kevin

There is nothing wrong with having seperate functions in seperate files.

The only mistake was that you did not have header file(s) that defined the prototypes for each function and include the header file(s) when needed.

Follow the suggestions of aCynic2 in using tokens.

The tokenizing will get the next pattern of characters based on a delimiter (in this case a blank). A string will be returned, get the first character of the string. Call the token function again and it will advance and return the next token. Continue until the end of the line or buffer.

 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
Originally posted by: Gamingphreek
I thought as well as separate functions that good programming practices suggested separate files as well. Obviously I was wrong. After combining all the functions into one gigantic file everything works
No. Your first thought was right. The prototype from the first file could have simply been cut and pasted into the second file. Or much better would be to create a separate header file for prototypes - one that contains no code and is included in code files. Modularization is a good thing. Depending on how large this app grows, having it all in one file will become unmanageable. One or a few functions in separate files also makes unit testing easier and encourages decoupling. Once the functions in a file are thoroughly tested, they won't have to be looked at again and again. Remember, habits formed now will last a long time. This applies to useful code commenting also.

Such as:

prototypes.h:
void calc(int& v);
void display(int v);

file1.cpp:
#include "prototypes.h"

/*
* calculate the ... and display it
*/
void main()
{
int var;

calc(var);
display(var);
}

file2.cpp:
#include "prototypes.h"

void calc(int& v)
{
...
}

file3.cpp:
#include "prototypes.h"

void display(int v)
{
...
}
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Right now I have this:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
string filename, Output_File;

// Opens and reads the file which the user specifies
ifstream Original_Doc;
cout << "Please enter the file name\n";
cin >> filename;
Original_Doc.open(filename.c_str());

// If file cannot be found returns an error and exits
if (!Original_Doc)
{
cout << "Unable to open file Original_Doc";
exit (1);
}

else
{
//This creates a new file where the user specifies the name
ofstream New_Doc;
cout << "What name would you like to output this file to?\n";
cin >> Output_File;
New_Doc.open(Output_File.c_str());
}

I haven't slept in 2 Days due to Engineering Tests and Homework and projects, so I can't think for myself at all. Would I read a line from the file into a string like this:
Original_Doc.getline()

and then from that I set a delimiter to the eof() limit and run a tokenizer somehow?

-Kevin
 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
main()
{
char str1[] = " \t A Line With\t Seven Words In It. ";
char letters[128];

char* l = letters; //FIXME: check boundaries
for (const char* s = str1; *s; )
{
//skip the whitespace before the next word
for ( ; *s; s++)
{
if (*s != ' ' && *s != '\t')
break;
}

*l++ = *s++; //the first letter of the word

//skip the rest of the letters of the word
for ( ; *s; s++)
{
if (*s == ' ' || *s == '\t')
break;
}
}
*l = 0;

printf("%s: \"%s\"\n", letters, str1);

return 0;
}


Did "Attach Code" ever work?

For some reason, I'm having trouble entering the include for stdio.h with angle brackets even from the keyboard - I get the "Ooops! You encountered an error" when previewing.
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Thanks so much; however, I need to read a string in from a txt file and get it to do that. So far I have:

#include <fstream>
#include <string>
#include <iostream>
using namespace std;

int main()
{
string filename, Output_File, TXT_File, str, str1, str2, str3, str4, str5, F_L;
size_t pos, X=0;
int i = 0;

// Opens and reads the file which the user specifies
ifstream Original_Doc;
cout << "Please enter the file name\n";
cin >> filename;
Original_Doc.open(filename.c_str());

// If file cannot be found returns an error and exits
if (!Original_Doc)
{
cout << "Unable to open file Original_Doc";
exit (1);
}

getline(Original_Doc, TXT_File);

int MAX = TXT_File.size();

for (i=0; i<=MAX; i++)
{
X = TXT_File.find(' ');
cout << X << endl;
}


I haven't done anything else but homework today-- lemme tell you college HW sucks!

Edit: I am so drained right now I don't even know what I am stuck on. Can someone tell me what I need to do to finish this piece of crap (Or give me prompts that are on the path to finishing)?
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Originally posted by: seemingly random
You're almost there. Why treat Original_Doc differently than cin?

What do you mean? I know with Original_Doc I can use it like cin and write things to the file stored there, but I don't know why I would need to do that.

-Kevin