I'm sorry for the vague title, but I don't know what else to say.
Here is my program:
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int
main (int argc, char **argv)
{
//string fin;
string ttl, dscp, ext;
string west = "w";
string east = "e";
string north = "n";
string south = "s";
int numRooms;
//string argv[1] = filename;
class Room
{
public:string title;
string description;
string exits;
int exitWest = -1;
int exitNorth = -1;
int exitEast = -1;
int exitSouth = -1;
int numExits;
};
ifstream fin;
fin.open (argv[1]);
//cin.ignore();
int t = 0;
while (fin)
{
string tilde;
string tester;
tester = "~";
cin >> tilde;
if (tilde == tester)
{
t = t + 1;
}
(numRooms = t / 3);
}
Room *roomArrayPtr = new Room[numRooms];
fin.clear ();
while (fin)
{
for (int l = 0; l < numRooms; l++)
{
getline (fin, ttl, '~');
roomArrayPtr[l].title = ttl;
getline (fin, dscp, '~');
roomArrayPtr[l].description = dscp;
getline (fin, ext, '~');
stringstream sin;
sin << ext;
string x;
int y;
while (sin >> x >> y)
{
if (x == west)
{
roomArrayPtr[l].exitWest = y;
}
if (x == south)
{
roomArrayPtr[l].exitSouth = y;
}
if (x == north)
{
roomArrayPtr[l].exitNorth = y;
}
if (x == east)
{
roomArrayPtr[l].exitEast = y;
}
}
sin.clear ();
int numext;
numext = (ext.size ()) / 3;
roomArrayPtr[l].numExits = numext;
}}
//(read in file again populate roomarrayptr w while loop and getline)
//roomArrayPtr[index].title = line;
//roomArrayPtr[index].description=line;
//close files, while loop that reads in user input from stdin, delete pointers w (delete[] roomArrayPtr;)
//if (exitsarray[i].size() > 2){
char command;
char newcommand;
cout << ">";
cin >> command;
int currentroom = 0;
int newroom;
bool keepgoing = true;
//string dir1;
while (keepgoing)
switch (command)
{
// stringstream ss;
case 'l':
cout << roomArrayPtr[currentroom].title << endl;
cout << roomArrayPtr[currentroom].description << endl;
cout << endl;
//if (roomArrayPtr[currentroom].numExits < 2) {
cout << "Exits: ";
if (roomArrayPtr[currentroom].exitWest > -1)
{
cout << "w";
}
if (roomArrayPtr[currentroom].exitNorth > -1)
{
cout << "n";
}
if (roomArrayPtr[currentroom].exitSouth > -1)
{
cout << "s";
}
if (roomArrayPtr[currentroom].exitEast > -1)
{
cout << "e";
}
/*else {
cout << "Exits: " ;
for (int k = 0; k < numExits; k++){
cout << exitdirection << " ";
}
}*/
//string newcommand;
cin >> newcommand;
//int newroom;
switch (newcommand)
{
case 'w':
if (roomArrayPtr[currentroom].exitWest == -1)
{
cout << "You can't go WEST!" << endl;
}
else
{
newroom = roomArrayPtr[currentroom].exitWest;
cout << "You moved WEST." << endl;
}
break;
case 'e':
if (roomArrayPtr[currentroom].exitEast == -1)
{
cout << "You can't go EAST!" << endl;
}
else
{
newroom = roomArrayPtr[currentroom].exitEast;
cout << "You moved EAST." << endl;
}
break;
case 'n':
if (roomArrayPtr[currentroom].exitNorth == -1)
{
cout << "You can't go NORTH!" << endl;
}
else
{
newroom = roomArrayPtr[currentroom].exitNorth;
cout << "You moved NORTH." << endl;
}
break;
case 's':
if (roomArrayPtr[currentroom].exitSouth == -1)
{
cout << "You can't go SOUTH!" << endl;
}
else
{
newroom = roomArrayPtr[currentroom].exitSouth;
cout << "You moved SOUTH." << endl;
}
break;
}
break;
case 'q':
keepgoing = false;
return 0;
break;
currentroom = newroom;
}
}
Whenever I run it, it compiles, but nothing happens. I tried putting in some random cout << "testing1" "testing2" scattered throughout, but none of those even showed up. I put it immediately after the { after int main and it still didn't show up. What's going on?
I tried putting in some random cout << "testing1" "testing2" scattered throughout, but none of those even showed up. I put it immediately after the { after int main and it still didn't show up.
you are reading the wrong file
int t = 0;
while (fin)
{
string tilde;
string tester;
tester = "~";
cin >> tilde; <<<<===== i assume you mean fin
if (tilde == tester)
{
t = t + 1;
}
(numRooms = t / 3);
}
Related
I was able to make a program that has a menu on it. At first, the program is running smoothly. The problem starts to appear when, at a certain point in the program, the user starts inputting something and then navigating the menus again.
Sometimes it will print out integers/characters that were previously entered by the user when pressing the ENTER key in the menu.
I managed to block the inputs with cin.ignore(). But how can I eliminate the display of the (I don't know if its excess or garbage) inside the input buffer whenever the user presses the LEFT and RIGHT arrow keys? Or I think there's even one for UP and DOWN arrow key?
int main()
{
color(FG_WHITE);
bool progFinished = false;
while (!progFinished) {
cout << "[ACTIVITY FOR 26 FEBRUARY 2018]\n";
vector<string> menu = { "Currency Converter","Student Information System" };
int choice = menu_std(menu, FG_YELLOW, false);
bool currencyFinished = false;
bool studentFinished = false;
switch (choice) {
case 0:
while (!currencyFinished) {
cout << "\n\n[CURRENCY CONVERT]\n";
vector<string> curr_menu = { "PHP TO OTHERS","USD TO PHP","YEN TO PHP","EUR TO PHP","BHD TO PHP" };
int currency = menu_std(curr_menu, FG_YELLOW, false);
double value = validation("\nEnter the value", 0, false);
CurrencyConverter convert(currency, value);
cout << "\n\nConvert again? ";
currencyFinished = !menu_yn(FG_YELLOW);
}
system("cls");
break;
case 1:
Studentv2 stud;
stud.LoadFile();
while (!studentFinished) {
cout << "\n\n[STUDENT INFO SYSTEM]\n";
vector<string> stud_menu = { "Add Student","View All Students" };
int stud_choice = menu_std(stud_menu, FG_YELLOW, false);
switch (stud_choice) {
case 0:
stud.AddStudent();
cout << "\n\nDo you want to continue with the Student Info System? ";
stud.SaveFile();
studentFinished = !menu_yn(FG_YELLOW);
break;
case 1:
stud.DisplayStudentInformation();
cout << "\n\nDo you want to continue with the Student Info System? ";
studentFinished = !menu_yn(FG_YELLOW);
break;
}
}
system("cls");
break;
}
cout << "\nContinue using the program? ";
progFinished = !menu_yn(FG_YELLOW);
system("cls");
}
cout << "Press any key to exit.\n";
cin.get();
return 0;
}
Code snippets for some of the functions I used inside my program.
Function menu_std():
int menu_std(std::vector<std::string> &menu, WORD atrribute, bool argument)
{
sleep_for(milliseconds(150));
int elements = menu.size();
int elements_max_size = menu.size() - 1;
int menu_position = 0;
COORD position = getcoordinates();
while (true) {
for (int i = 0; i < elements; i++) {
move(position.X, position.Y + i);
if (i == menu_position) {
color(atrribute);
std::cout << menu[i];
color(FG_WHITE);
}
else {
std::cout << menu[i];
}
}
while (true) {
if (GetAsyncKeyState(VK_UP) < 0 && menu_position != 0) {
menu_position--;
break;
}
if (GetAsyncKeyState(VK_DOWN) < 0 && menu_position != elements_max_size) {
menu_position++;
break;
}
if (GetAsyncKeyState(VK_RETURN) < 0) {
std::cin.ignore(10000, '\n');
if (argument == true) {
sleep_for(milliseconds(100));
COORD previous = getcoordinates();
std::cout << "\n\nAre you sure? ";
bool sure = menu_yn(atrribute);
if (sure) {
clear_output(previous);
return menu_position;
}
else {
clear_output(previous);
break;
}
}
else {
return menu_position;
}
}
sleep_for(milliseconds(50));
}
sleep_for(milliseconds(175));
}
}
Function menu_yn():
bool menu_yn(WORD attribute)
{
sleep_for(milliseconds(150));
COORD currentpos = getcoordinates();
int pos = 0;
while (true) {
move(currentpos);
switch (pos) {
case 0:
color(attribute);
std::cout << "YES";
color(FG_WHITE);
std::cout << " NO";
break;
case 1:
std::cout << "YES";
color(attribute);
std::cout << " NO";
color(FG_WHITE);
break;
}
while (true) {
if (GetAsyncKeyState(VK_RIGHT) < 0 && pos != 1) {
pos++;
break;
}
if (GetAsyncKeyState(VK_LEFT) < 0 && pos != 0) {
pos--;
break;
}
if (GetAsyncKeyState(VK_RETURN) < 0) {
std::cin.ignore(10000, '\n');
if (pos == 0) {
return true;
}
else if (pos == 1) {
return false;
}
}
}
sleep_for(milliseconds(175));
}
}
Function validation()
double validation(std::string question, double limit, bool argument)
{
double userInput;
while (true) {
std::cout << question << " >> ";
while (!(std::cin >> userInput)) {
std::cout << "Error: Non-integer input. " << question << " >> ";;
std::cin.clear();
std::cin.ignore(256, '\n');
}
std::cin.ignore(10000, '\n');
while (userInput > limit && limit != 0) {
std::cout << "Error: Input out-of-bounds. " << question << " >> ";
std::cin >> userInput;
std::cin.ignore(10000, '\n');
}
if (argument) {
return (userInput <= 0) ? 1 : userInput;
}
else {
return userInput;
}
}
}
Screenshot of the problem:
There are numbers appearing whenever I press enter on the menu. I suspect that it comes whenever the user presses the arrow keys before entering the enter.
I'm working on ATM machine code in C++. I know it is best to use a binary file and classes so that each account has exact same size in file even if it is empty. I'm looking for something very very simple.
I'm having problem with parsing comma delimited values in .txt file. Sometime I get values, sometime I get just the first value. I have spent days to figure out it's solution. I have searched the internet and stackoverflow almost every answer used vectors or other stuff.
I didn't use strcmp for array comparison instead created my own function to compare character to character.
From the look of the code, it seems I have done a lot just a little push is required. Kindly have a look at my code and let me know where am I wrong. I want to eliminate semantic error! And to have the following code amended for the achievement of expected results.
#include<iostream>
#include<conio.h>
#include<fstream>
#include<cstring>
#include<string>
#include<sstream>
#include<stdlib.h>
using namespace std;
void checkBalance();
void createAccount();
void deposit();
void withdraw();
void transfer();
void login();
void mainScreen();
void menuScreen();
// For file handling
void saveAccountToFile();
bool checkAccountExists(char*);
void loadAccount(char *);
void loadBeneficiary(char*);
bool compare(char *, char *);
const char * FILE_NAME = "accounts.txt";
char username[50];
char pin[5];
double balance = 0.0;
// Beneficiary Variables
char b_username[50];
char b_pin[5];
double b_balance = 0.0;
void menuScreen()
{
char opt;
do {
system("cls");
cout << "\n\n\t\t CMD - ATM";
cout << "\n\n\tMENU";
cout << "\n\n\t01. Check Balance";
cout << "\n\n\t02. Withdraw";
cout << "\n\n\t03. Deposite";
cout << "\n\n\t04. Transfer to Another account";
cout << "\n\n\t05. Logout";
opt = _getch();
switch (opt)
{
case '1':
checkBalance();
break;
case '2':
withdraw();
break;
case '3':
deposite();
break;
case '4':
transfer();
case '5':
cout << "\n\nThanks for using CMD ATM. Press any key to exit...";
_getch();
exit(1);
default:
cout << "\a\n\nIncorrect input. Press any key to try agian!";
_getch();
}
} while (opt != '5');
}
void mainScreen()
{
char opt;
do {
system("cls");
cout << "\n\n\t\t CMD - ATM";
cout << "\n\n\tMENU";
cout << "\n\n\t01. Create Account";
cout << "\n\n\t02. Login";
cout << "\n\n\t03. Exit";
opt = _getch();
switch (opt)
{
case '1':
createAccount();
break;
case '2':
login();
break;
case '3':
exit(1);
default:
cout << "\a\n\nIncorrect input. Press any key to try agian!";
_getch();
}
} while (opt != '3');
}
void saveAccountToFile() {
ofstream outFile;
outFile.open(FILE_NAME, ios::app);
if (outFile.is_open()) {
outFile << username << ',' <<
pin << ',' << balance << "\n";
}
outFile.close();
}
bool checkAccountExists(char * userName)
{
int i = 0;
char temp[50] = {'0'};
ifstream inFile;
string line;
inFile.open(FILE_NAME);
if (inFile.is_open()) {
while (getline(inFile, line)) {
stringstream ss(line);
string value;
while (getline(ss, value, ',')) {
if (i == 0) {
strcpy(temp,value.c_str());
if(compare(userName, temp)){
cout << "closing";
getchar();
inFile.close();
return true;
}
i = 1;
}
else if (i == 1) {
i = 2;
}
else if (i == 2) {
i = 0;
}
}
}
inFile.close();
}
return false;
}
void loadAccount(char * userName)
{
int i = 0;
ifstream inFile;
string line;
inFile.open(FILE_NAME);
if (inFile.is_open()) {
while (getline(inFile, line)) {
stringstream ss(line);
string value;
while (getline(ss, value, ',')) {
if (i == 0) {
strcpy(username, value.c_str());
i = 1;
}
else if (i == 1) {
strcpy(pin, value.c_str());
i = 2;
}
else if (i == 2) {
balance = atof(value.c_str());
i = 0;
}
if(compare(username,userName))
{
inFile.close();
break;
}
}
}
inFile.close();
}
}
void loadBeneficiary(char * userName)
{
int i = 0;
ifstream inFile;
string line;
inFile.open(FILE_NAME);
if (inFile.is_open()) {
while (getline(inFile, line)) {
stringstream ss(line);
string value;
while (getline(ss, value, ',')) {
if (i == 0) {
strcpy(b_username, value.c_str());
i = 1;
}
else if (i == 1) {
strcpy(b_pin, value.c_str());
i = 2;
}
else if (i == 2) {
b_balance = atof(value.c_str());
i = 0;
}
if(strcmp(b_username, userName)){
inFile.close();
break;
}
}
}
inFile.close();
}
}
void login()
{
char tempName[50];
char tempPin[5];
cout << "\n\nEnter user name: ";
cin.getline(tempName, sizeof(tempName));
if (checkAccountExists(tempName)) {
cout << "\n\nEnter user pin: ";
cin.getline(tempPin,sizeof(tempPin));
loadAccount(tempName);
if (compare(tempPin, pin)) {
cout << "\n\nLog in successfull!";
cout << "\n\nPress any key to continue...";
_getch();
menuScreen();
}else {
cout << "User name or pin incorrect!";
_getch();
}
}
else {
cout << "\n\nRecord not found. Press any key to continue...";
_getch();
}
}
void checkBalance()
{
system("cls");
cout << "\n\n\tUser name = " << username;
cout << "\n\n\tBalance = " << balance;
_getch();
}
bool compare(char * msg1, char * msg2){
int count = 0;
int size = sizeof(msg1);
if(sizeof(msg2) == size){
for(int i = 0; i < size; i++){
if(msg1[i]==msg2[i]){
count++;
}
}
if(count == size)
return true;
}
return false;
}
int main(int argc, char** argv) {
mainScreen();
getchar();
return 0;
}
TXT FILE
jhon,5155,99999.99
bot,4414,232323
theta,2111,34234
Regarding how to simplify the reading loop, how about something like
while (getline(inFile, line)) {
stringstream ss(line);
string value;
getline(ss, value, ',');
string name = value;
getline(ss, value, ',')
string pin = value;
getline(ss, value)
double balance = stod(value);
// Now use the name, pin and balance some way...
}
For when you only need the name you don't need to have the second and third getline call, just the first.
Also consider using structures to store the name, pin and balance, and then having a container (for example a vector) to stor the structures.
Also in the name of simplicity I suggest you read the file only once. Then loop over the vector to find the data you need.
Here is an alternative without using stringstream
while(getline(inFile,line,',')){
strcpy(username, line.c_str());
getline(inFile,line,',');
strcpy(pin, line.c_str());
getline(inFile,line,'\n');
balance = atof(line.c_str());
if(compare(username,userName)){
inFile.close();
break;
}else
continue;
}
me again. I have a problem. I tried to run this code and when I compile it, the compiler shows no errors, but after running it, the program crashes. HELP!!!
What the program should do:
It searches trough the data of deseases and desease codes of the WHO ( World Health Organisation). Two csv files are given (english and german version) and you have a choice to search in english and in german. I think that the program chrashes when it tries to load the char*-s from the csv file.
It's driving me crazy.
Here is also the link where you can find the whole project:
LINK
Code:
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <list>
#include <algorithm>
using namespace std;
enum KEY {code, disease} key;
enum LANGUAGE {english, deutsch} language;
char* buffer;
struct lst
{
char *_code;
char *_disease;
};
streamsize _buffer = 10000;
bool isPartOf(char* word, char* sentence)
{
unsigned int i=0;
unsigned int j=0;
for(;i < strlen(sentence); i++)
{
if(sentence[i] == word[j])
{
j++;
}
}
if(strlen(word) == j)
return true;
else
return false;
}
void printList(list<lst>* LST)
{
for(list<lst>::iterator i= LST->begin(); i != LST->end(); i++)
{
cout << i->_code << '\t' << i->_disease << endl;
}
}
list<lst>* makeList(char* fileName)
{
int i,j;
fstream ICDcsv;
ICDcsv.open(fileName);
list<lst>* new_list = new list<lst>;
if(ICDcsv.is_open())
{
while(ICDcsv)
{
lst* x = new lst;
ICDcsv.getline(buffer,_buffer);
i = 0;
j = 0;
while (buffer[i] != ';')
{
x->_code[i] = buffer[i];
i++;
}
i++;
while (buffer[i] != ';')
{
x->_disease[j] = buffer[i];
i++;
j++;
}
(*new_list).push_back(*x);
}
ICDcsv.close();
}
else{cerr << "Error: file error" << endl;}
return new_list;
}
list<lst>* listSearch(list<lst> *LST,char* wrd,KEY key)
{
switch(key)
{
case code:
for(list<lst>::iterator i = LST->begin(); i != LST->end(); i++)
{
if(!isPartOf(wrd, ( *i )._code))
{
delete [] i->_code;
delete [] i->_disease;
delete &(*i);
}
} //i->
break;
case disease:
for(list<lst>::iterator i = LST->begin(); i != LST->end(); i++)
{
if(!isPartOf(wrd, ( *i )._disease))
{
delete [] i->_code;
delete [] i->_disease;
delete &(*i);
}
}
break;
}
return LST;
}
int main()
{
int choice;
int program_end=1;
char* _file;
char* Search;
cout << "World Health Institution (WHO)/Weltgesundheitsorganisation" << endl;
cout << "International Statistical Classification of Diseases and Related Healt Problems (ICD)/Internationale statistische Klassifikation der Krankheiten und verwandter Gesundheitsprobleme" << endl;
cout << "1 english" << endl;
cout << "2 deutsch" << endl;
cout << "your choice/Ihre Auswahl: ";
cin >> choice;
cin.clear();
if(choice == 1)
language = english;
else
language = deutsch;
switch (language)
{
case english:
{
_file = "care_icd10_en.csv";
break;
}
case deutsch:
{
_file = "care_icd10_de.csv";
break;
}
}
cout << "0 end/Ende" << endl;
cout << "1 search for ICD code (e.g. K52.9)/Suche nach ICD Kode (Beispiel K52.9)" << endl;
cout << "2 search for desease (e.g. Ebola)/Suche nach Krankheit (Beispiel Ebola)" << endl;
cout << "your choice/Ihre Auswahl: ";
cin >> program_end;
cin.clear();
switch(program_end)
{
case 0: break;
case 1:
key = code;
cout << "to search for ICD code/zu suchender ICD Kode: ";
break;
case 2:
key = disease;
cout << "to search for deseade/zu suchende Krankheit: ";
break;
}
if(program_end != 0)
{
cin >> Search;
list<lst>* test = makeList(_file);
list<lst>* test2 = listSearch(test,Search,key);
printList(test);
}
return 0;
}
This question already has an answer here:
invalid conversion from 'int' to 'char *'
(1 answer)
Closed 8 years ago.
Hi I am suppose to create a program to read a .txt file and save the information in the txt to a struct and enum (have to use enum. as it compulsory) then prints out the information after rearranging the words. eg
Sarah
Wonderland
Libra 2 - 10 - 1993
3
I want to...
I hope to...
............
TO
My name is sarah
my nationality is wonderland
my bday is 2 october 1993
I am a libra
I have 3 wishes
1. I want to...
2. I hope to...
3. ............
I have encountered similar error C2664 for getHoroNum and checkHoro stating that it cant convert parameter 1 from 'char [80]' to 'char'. Please help! Thanks a lot in advance!
#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <fstream>
#include <string>
using namespace std;
const int MAX = 80;
const int MAXNO = 10;
enum Zodiac
{
Aquarius, Pisces, Aries, Taurus,
Gemini, Cancer, Leo, Virgo,
Libra, Scorpio, Sagittarius, Capricorn
};
struct Date
{
Zodiac sign;
int day;
int month;
int year;
};
struct Student
{
char name [MAX];
char nationality [MAX];
Date birthDate;
int no; // no of other messages
char wishMessage [MAXNO][MAX];
};
void myInfo (fstream&, char [], Student&);
// The above function reads information from the text file
// and store the information in a structure reference parameter
void printOut(Student , char[], char[]);
void getHoroNum(char , Student&);
void checkHoro (char , Student);
void getMth (int, char []) ;
int main()
{
fstream infile;
char fName[MAX];
char horo [MAX];
char fHoro[MAX];
char mth[MAX];
int month;
Student info;
cout << "Enter your info file name: ";
cin >> fName;
cout << endl;
month = info.birthDate.month;
myInfo(infile, fName, info);
getHoroNum(horo, info);
checkHoro(fHoro, info);
getMth(month, mth);
printOut(info, );
}
void myInfo (fstream& infile, char fName[], Student& info)
{
infile.open(fName, ios::in);
char temp[MAX];
char horo[MAX];
if(!infile)
{
cout << "Error file not found!" << endl;
exit(0);
}
infile.getline(info.name, MAX);
infile.getline(info.nationality,MAX);
infile >> horo
>> info.birthDate.day
>> temp
>> info.birthDate.month
>> temp
>> info.birthDate.year;
infile >> info.no;
for(int i=0; i < info.no ;i++)
{
infile.getline(info.wishMessage[i], MAX);
}
infile.close();
cout << "Successfully readed!" << endl;
}
void getHoroNum(char horo[], Student& info)
{
if (strcmp (horo,"Aquarius"))
{
info.birthDate.sign = Aquarius;
}
else if (strcmp(horo,"Pisces"))
{
info.birthDate.sign = Pisces;
}
else if (strcmp(horo,"Aries"))
{
info.birthDate.sign = Aries;
}
else if (strcmp(horo,"Taurus"))
{
info.birthDate.sign = Taurus;
}
else if (strcmp(horo,"Gemini"))
{
info.birthDate.sign = Gemini;
}
else if (strcmp(horo,"Cancer"))
{
info.birthDate.sign = Cancer;
}
else if (strcmp(horo,"Leo"))
{
info.birthDate.sign = Leo;
}
else if (strcmp(horo,"Virgo"))
{
info.birthDate.sign = Virgo;
}
else if (strcmp(horo,"Libra"))
{
info.birthDate.sign = Libra;
}
else if (strcmp(horo,"Scorpio"))
{
info.birthDate.sign = Scorpio;
}
else if (strcmp(horo,"Sagittarius"))
{
info.birthDate.sign = Sagittarius;
}
else if (strcmp(horo,"Capricorn"))
{
info.birthDate.sign = Capricorn;
}
}
void checkHoro (char fHoro[], Student info)
{
if (info.birthDate.sign == Aquarius)
{
fHoro = "Aquarius";
}
else if (info.birthDate.sign == Pisces)
{
fHoro = "Pisces";
}
else if (info.birthDate.sign == Aries)
{
fHoro = "Aries";
}
else if (info.birthDate.sign == Taurus)
{
fHoro = "Taurus";
}
else if (info.birthDate.sign == Gemini)
{
fHoro = "Gemini";
}
else if (info.birthDate.sign == Cancer)
{
fHoro = "Cancer";
}
else if (info.birthDate.sign == Leo)
{
fHoro = "Leo";
}
else if (info.birthDate.sign == Virgo)
{
fHoro = "Virgo";
}
else if (info.birthDate.sign == Libra)
{
fHoro = "Libra";
}
else if (info.birthDate.sign == Scorpio)
{
fHoro = "Scorpio";
}
else if (info.birthDate.sign == Sagittarius)
{
fHoro = "Sagittarius";
}
else if (info.birthDate.sign == Capricorn)
{
fHoro = "Capricorn";
}
}
void getMth (int month, char mth[] )
{
switch (month)
{
case 1:
{
mth = "January";
break;
}
case 2:
{
mth = "February";
break;
}
case 3:
{
mth = "March";
break;
}
case 4:
{
mth = "April";
break;
}
case 5:
{
mth = "May";
break;
}
case 6:
{
mth = "June";
break;
}
case 7:
{
mth = "July";
break;
}
case 8:
{
mth = "August";
break;
}
case 9:
{
mth = "September";
break;
}
case 10:
{
mth = "October";
break;
}
case 11:
{
mth = "November";
break;
}
case 12:
{
mth = "December";
break;
}
}
}
void printOut(Student info, char mth[], char fHoro[])
{
cout << "My name is " << info.name << endl;
cout << "My nationality is " << info.nationality << endl;
cout << "My date of birth is " << info.birthDate.day
<< " " << mth << " "
<< info.birthDate.year << endl;
cout << "I am a" << fHoro << endl;
cout << "\nI have " << info.no << " wishes:" << endl;
for(int i=0; i < info.no ;i++)
{
cout << i << ". " << info.wishMessage[i];
}
}
You have this function definition:
void getHoroNum(char horo[], Student& info)
and before you have this declaration:
void getHoroNum(char , Student&);
latter should be replaced by:
void getHoroNum(char* , Student&);
I am a little curious why the program I wrote on Xcode with a mac runs fine, but when I try compiling on a windows system with visual studio,
I get following error:
c:\users\bryan\documents\visual studio
2010\projects\new\new\new.cpp(172): error C3861: 'transform':
identifier not found.
When I write transform anywhere in the program, in fact, it says the same message, as if transform is not a part of the std namespace. Here is my code if you would like to see for yourself:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <cctype>
using namespace std;
string getInput ();
ifstream * openInFile ();
int getShiftValue ();
void cypherMenu ();
void menu ();
string shiftCharacters (int shiftNum, ifstream * inFile);
string getOutput ();
ofstream * openOutFile ();
void printSentence (string outData, ofstream * outFile);
void notOption (string optionString);
string capitalize (string choice);
string option ();
int main() {
ifstream * inFile;
ofstream * outFile;
string inFileName, outFileName, outData, optionString, capOptionString;
int shiftNum = 0;
bool isOption = false;
while (capOptionString.compare("2") != 0 ||
capOptionString.compare("QUIT") != 0) {
do {
menu();
optionString = option();
capOptionString = capitalize(optionString);
if (capOptionString.compare("1") == 0 || capOptionString.compare("CAESAR")
== 0) {
isOption = true;
}
else if (capOptionString.compare("2") == 0 ||
capOptionString.compare("QUIT") == 0) {
isOption = false;
return 0;
}
else {
notOption(optionString);
}
}
while (!isOption);
cypherMenu();
inFile = openInFile();
shiftNum = getShiftValue();
outData = shiftCharacters(shiftNum, inFile);
inFile->clear();
inFile->close();
outFile = openOutFile();
printSentence(outData, outFile);
outFile->clear();
outFile->close();
}
return 0;
}
// Input Functions
string getInput () {
cout << "Enter an input file name: ";
string inFileName;
getline(cin, inFileName);
return inFileName;
}
string getOutput () {
string outFileName;
cout << "Enter an output file name: ";
getline(cin, outFileName);
cout << endl;
return outFileName;
}
ifstream * openInFile () {
ifstream * inFile;
bool isGood = false;
string inFileName;
inFile = new ifstream;
do {
inFileName = getInput();
inFile->open(inFileName.c_str());
if (inFile->fail()) {
cout << "Couldn't open file" << endl;
}
else {
isGood = true;
}
}
while (!isGood);
return inFile;
}
ofstream * openOutFile () {
ifstream testStream;
ofstream * outFile;
bool isUnique = false;
string fileName;
do {
fileName = getOutput();
testStream.clear();
testStream.open(fileName.c_str(), ios_base::in);
if (testStream.good()) {
cout << "The file already exists, please choose another"
<< endl;
testStream.clear();
testStream.close();
}
else {
isUnique = true;
testStream.clear();
testStream.close();
}
}
while (!isUnique);
outFile = new ofstream;
outFile->open(fileName.c_str());
return outFile;
}
int getShiftValue () {
int shiftNum;
string trash;
cout << "Please enter shift value: ";
cin >> shiftNum;
getline(cin, trash);
return shiftNum;
}
string option () {
string optionString;
getline(cin, optionString);
cout << endl;
return optionString;
}
// Data manipulation functions
string shiftCharacters (int shiftNum, ifstream * inFile){
string inData, outData, trash, tempString;
char outChar;
int idx = 0, idxTwo = 0, newLines = 0;
stringstream outSentence;
do {
while (getline(* inFile, inData, '\n')) {
for (idx = 0; idx <= inData.length() - 1; idx++) {
if (inData[idx] >= 'a' && inData[idx] <= 'z') {
outChar = (((inData[idx] - 'a') + shiftNum) % 26) +
'a';
outSentence << outChar;
}
else if (inData[idx] >= 'A' && inData[idx] <= 'Z') {
outChar = (((inData[idx] - 'A') + shiftNum) % 26) +
'A';
outSentence << outChar;
}
else {
outChar = inData[idx];
outSentence << outChar;
}
}
outSentence << '\n';
newLines++;
}
}
while (!inFile->eof());
for (idxTwo = 0; idxTwo <= newLines + 1; idxTwo++) {
getline(outSentence, tempString);
outData.append(tempString);
outData += '\n';
}
return outData;
}
string capitalize (string choice) {
string outString;
outString.resize(choice.length());
transform(choice.begin(), choice.end(), outString.begin(), ::toupper);
return outString;
}
// Output funcitons
void cypherMenu () {
cout << "C A E S A R C Y P H E R P R O G R A M" << endl
<< "========================================" << endl;
return;
}
void printSentence (string outData, ofstream * outFile) {
int idx = 0;
char outChar;
stringstream outString;
outString << outData;
for (idx = 0; idx <= outData.length() - 1; idx++) {
outChar = outString.get();
outFile->put(outChar);
}
}
void menu () {
cout << "Available Options: " << endl
<< "1. CAESAR - encrypt a file using Caesar Cypher" << endl
<< "2. QUIT - exit the program" << endl << endl
<< "Enter a keyword or option index: ";
return;
}
void notOption (string optionString) {
cout << optionString << " is an unrecognized option, try again" << endl
<< endl;
return;
}
The problem is the transform under the capitalize funciton. It doesn't seem to know transform at all, I even tried to write std:: before it, but it said that there is no member called transform. Is there a problem with my compiler, or is transform even used anymore since i downloaded the new studio, or maybe it is just bad practice ?
As you would discover from reading documentation (e.g. http://en.cppreference.com/w/cpp/algorithm/transform), std::transform is defined in the standard header <algorithm>, which you aren't including.