My confused mind is now real stuck!
How do you add ".txt" to a char?
I wanted to do something like this:
charA = charB + ".txt"
but since it's a char it aint working.
Somebody please help me....
Printable View
My confused mind is now real stuck!
How do you add ".txt" to a char?
I wanted to do something like this:
charA = charB + ".txt"
but since it's a char it aint working.
Somebody please help me....
Well, I'm assuming it's a char array, so just use strcat.
*Note : you may want check 'filext', to see if the user has already entered an extenion. btw - code was written in CCode:#include "stdio.h"
#include "conio.h" // getch ()
#include "windows.h" // strlen()
int get_strlen(char * string);
int main()
{
char filext[80] = { NULL }; // { NULL } sets all the subscript to NULL
int idx = 0;
printf("\n Type a filename w/out an ext. : ");
gets(filext);
printf("\n filext = [ %s ] ",filext);
// strlen() returns the number of chars in filext
idx = strlen(filext);
// idx = get_strlen(filext);
filext[idx] = '.';
filext[++idx] = 't';
filext[++idx] = 'x';
filext[++idx] = 't';
printf("\n filext = [ %s ] ",filext);
printf("\n ------\n\n");
getch(); // press any key to continue
return 0;
}
// Use this is you don't have strlen() or windows.h
int get_strlen(char * str)
{
int nbr = 0;
// \0 = NULL
while(str[nbr] != '\0')
nbr++;
return nbr;
}
Ack!
All that for just .txt?
yikes....
Well thank you.
that sure is...advanced...:(
Well, I kinda made a sample program... all you gotta do is
--
- Get the number of chars typed(basically your finding out where the last char is in the array)
- From there you add the extention subscript by subscript
Its that simple.
I only added the strlen() and the get_strlen() thing in there just to make it easier.
cout << "enter the file path and the program will add the file extension." << endl;
char input[80];
cin >> input;
char ext[5] = ".txt";
char filename[86];
strcpy(filename, input);
strcat(filename, ext);
cout << "the full file name will be " << filename;
if you use string class from STL.
cout << "enter file name";
string filename;
cin >> filename;
filename = filename + ".txt";
hm.. i suggest everyone use the above methods (not mine) ...
I have never used strcat() before, it looks a lot better .. interesting.
For some reason the program just skips the code after the point defining and just restarts the loop.
I cant find any reason for the program to do so so i hoped you could help me a little more...
Code:#include <iostream>
#include <cstring>
#include <fstream>
#include <stdio.h>
int get_strlen(char * str)
{
int nbr = 0;
while (str[nbr] != '\0')
nbr++;
return nbr;
}
int main() {
char* filename;
int idx = 0;
int startoption;
char* saveoption;
char name[50];
int age;
char race[50];
char raceexample[10];
int classopt;
char* charclass;
char weapon;
int points;
points = 20;
int Str,Con,Int,Dex,Cha;
int strAdd,conAdd,intAdd,dexAdd,chaAdd;
int loopon;
loopon = 30;
while(loopon == 30){ //start loop
cout << "Hello and welcome to Daniel Bjorklunds RPG game.";
cout << "What would you want to do?\n";
cout << "1. Create a character\n";
cout << "2. Load an existing character\n";
cout << "3. Quit\n";
cin >> startoption;
switch(startoption) { //start switch
case 1:
cout << "\n-----CHARACTER CREATOR-----\n\n";
cout << "Choose a name for your character: ";
cin >> name;
cout << endl <<"You have chosen " << name << " as your character" << endl;
cout << "\n\aNext you need to choose an age for your character.\n";
cout << "The age depends upon your characters race (decided later).\n";
cout << "For example, humans can be from 15-50 while elves can be more than 2000 years old.\r";
cout << "Now choose an age: ";
cin >> age;
cout << endl <<"Your age is " << age << endl;
cout << "\n\aAnd now for the characters race.\n";
cout << "Choose whatever you want (within reasonable limits).\n";
cout << "\nExamples: Human, Elf, Dwarf, Goblin, Orc, Pixie\n";
cout << "Choose a race: ";
cin >> race;
cout << endl <<"Your race is " << race << endl;
cout << "Now for your characters class...\n";
cout << "Here are the classes you can choose from:\n\n";
cout << "1. Warrior, 2. Magician, 3. Ranger\n\n";
cout << "Now choose: ";
cin >> classopt;
if(classopt == 1) {
cout << endl << "You choose to be a Warrior!\n\n";
Str = 12;
Con = 12;
Int = 7;
Dex = 9;
Cha = 8;
charclass = "Warrior";
}
if(classopt == 2) {
cout << endl << "You choose to be a Magician!\n\n";
Str = 6;
Con = 8;
Int = 14;
Dex = 9;
Cha = 11;
charclass = "Magician";
}
if(classopt == 3) {
cout << endl << "You choose to be a Ranger!\n\n";
Str = 10;
Con = 10;
Int = 9;
Dex = 10;
Cha = 9;
charclass = "Ranger";
}
cout << "Now we are going to distribute some ability points.\n";
cout << "Right now your abilitys are:\n\n";
cout << "Strength (Str): " << Str;
cout << "\nConstitution (Con): " << Con;
cout << "\nDexterity (Dex): " << Dex;
cout << "\nIntelligence (Int): " << Int;
cout << "\nCharisma (Cha): " << Cha;
cout << "\n\nYou have " << points << " to distribute.\n";
cout << "You can only have a maximum of 18 points in each skill.\n\n";
cout << "Str: " << Str << " Points: " << points;
cout << "\n\nHow much would you like to add to your Strength?\n";
cout << "Add: ";
cin >> strAdd;
Str = Str + strAdd;
points = points - strAdd;
cout << endl << "Your Strength is now: " << Str << ".\n\n\n" << endl;
cout << "Con: " << Con << " Points: " << points;
cout << "\n\nHow much would you like to add to your Constitution?\n";
cout << "Add: ";
cin >> conAdd;
points = points - conAdd;
Con = Con + conAdd;
cout << endl << "Your Constitution is now: " << Con << ".\n\n\n" << endl;
cout << "Dex: " << Dex << " Points: " << points;
cout << "\n\nHow much would you like to add to your Dexterity?\n";
cout << "Add: ";
cin >> dexAdd;
Dex = Dex + dexAdd;
points = points - dexAdd;
cout << endl << "Your Dexterity is now: " << Dex << ".\n\n\n" << endl;
cout << "Int: " << Int << " Points: " << points;
cout << "\n\nHow much would you like to add to your Intelligence?\n";
cout << "Add: ";
cin >> intAdd;
Int = Int + intAdd;
points = points - intAdd;
cout << endl << "Your Intelligence is now: " << Int << ".\n\n\n";
cout << "Cha: " << Cha << " Points: " << points;
cout << "\n\nHow much would you like to add to your Charisma?\n";
cout << "Add: ";
cin >> chaAdd;
Cha = Cha + chaAdd;
points = points - chaAdd;
cout << endl << "Your Charisma is now: " << Cha;
cout << "\n\nDo you want to save this character?\n";
cin >> saveoption;
if(saveoption == "yes") {
filename = name;
idx = get_strlen(filename);
filename[idx] = '.';
filename[++idx] = 't';
filename[++idx] = 'x';
filename[++idx] = 't';
ofstream charfile(filename);
charfile << name << "\n";
charfile << age << "\n";
charfile << race << "\n";
charfile << charclass << "\n";
charfile << 1 << "\n";
charfile << 0 << "\n";
charfile << weapon << "\n";
charfile << Str << "\n";
charfile << Con << "\n";
charfile << Dex << "\n";
charfile << Int << "\n";
charfile << Cha;
charfile.close();
cout << "Your character has been succesfully saved in the file:\n\n";
cout << filename;
}
continue;
case 2:
cout << "That function is unavailable right now.\n\n";
continue;
case 3:
break;
}
break;
}
return 0;
}
filename is a char * and name is a char[]. If you try to assign name to filename you will probably give filename the address of name[0], which is not the same assigning name to filename. You cannot assign one c_style string to another using the assignment operator, ever. Use strcpy() in the cstring header file
char filename[50];
char name[50];
strcpy(filename, name);
Note that filename must have enough memory to hold the entire string in name in order for this to work. Declaring filename to be a pointer doesn't declare memory for holding a string, just a single char (unless you use dynamic memory to declare the pointer, but that's a different story).
Note also that in order to make name a string after adding .txt to it the way you did you need to add a terminating null char. As it is name is a straight char array, not a null terminated char array, so it can't be used at all, ever, as a string.
>char* saveoption;
Just a pointer. Should be:
char saveoption[5];
>if(saveoption == "yes") {
Should be:
if(strcmp(saveoption,"yes") == 0) {