C++ not getting all lines from .txt file when tokenising - c++
I am trying to read in my text file that contains book list.
However, when I output the file that I read in, I found there is some left out.
I'm wondering if the problem is happening when I try to tokenise the file.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <string>
#include <conio.h>
#include <cctype>
using namespace std;
typedef struct
{
int shelf;
int level;
} LOCATION_TYPE;
typedef struct
{
char isbn[14];
char author[40];
char title[40];
char publisher[40];
int year;
char genre[20];
char price[10];
char quantity[3];
LOCATION_TYPE location;
} BOOK_TYPE;
void menu();
void list(BOOK_TYPE book[], int total_book);
void search(BOOK_TYPE students[], int total_student);
int main(void)
{
BOOK_TYPE book[100];
ifstream input("booklist.txt"); // read input from a text file.
if (!input) // check error.
{
cout << "Error opening file.\n";
exit(100); // testing if the text file is connected
}
else
{
char delim[] = ",";
char temp[100];
int index = -1, choice;
input.getline(temp, 100, '\n');
while(input)
{
char* token = strtok(temp, delim);
strcpy(book[++index].isbn, token);
strcpy(book[index].author, strtok(NULL, delim));
strcpy(book[index].title, strtok(NULL, delim));
strcpy(book[index].publisher, strtok(NULL, delim));
book[index].year = atoi(strtok(NULL, delim));
strcpy(book[index].price, strtok(NULL, delim));
strcpy(book[index].quantity, strtok(NULL, delim));
book[index].location.shelf = atoi(strtok(NULL, delim));
book[index].location.level = atoi(strtok(NULL, delim));
strcpy(book[index].genre, strtok(NULL, delim));
// clear unwanted whitespace
if (input.peek() == '\n')
input.ignore(256, '\n');
// read next number
input.getline(temp, 100, '\n');
//token = strtok(book[++index], delim);
}
input.close();
system("cls");
menu();
cin >> choice;
while (choice != 0)
{
if (choice == 1)
{
list(book, index);
}
else if (choice == 2)
{
search(book, index);
}
else
{
system("cls");
cout << "Invalid input. Please key in number 0-4.\n";
system("pause");
}
system("cls");
menu();
cin >> choice;
}
}
return 0;
}
void menu()
{
cout << "UNDERGROUND BOOKS" << endl;
cout << "1. List all books record" << endl;
cout << "2. Search books" << endl;
cout << "3. Add books" << endl;
cout << "4. Delete books" << endl;
cout << "0. Exit" << endl;
cout << "What would you like to do?" << endl;
}
void list(BOOK_TYPE book[], int total_book)
{
system("cls");
for (int i = 0; i < total_book; i++)
{
cout << left << setw(15) << "ISBN" << setw(20) << "Author" << setw(35) << "Title"
<< setw(25) << "Publisher" << setw(7) << "Year" << setw(15) << "Genre"
<< setw(4) << "Qty" << setw(6) << "Price" << endl;
cout << "________________________________________________________________________________________________________________________________\n" << endl;
cout << left << setw(15) << book[i].isbn
<< setw(20) << book[i].author
<< setw(35) << book[i].title
<< setw(25) << book[i].publisher
<< setw(7) << book[i].year
<< setw(15) << book[i].genre
<< setw(4) << book[i].quantity
<< setw(6) << book[i].price << endl << endl
<< "(Located at shelf " << book[i].location.shelf << " level " << book[i].location.level << ")" << endl << endl;
cout << "________________________________________________________________________________________________________________________________" << endl;
}
system("pause");
}
void search(BOOK_TYPE book[], int total_book)
{
char keyword[55];
char* substring;
char tempKeyword[20];
system("cls");
// Prompt and get keyword to search student
cout << "Enter ISBN, book title or author name to search: ";
while (getchar() != '\n');
cin.getline(keyword, 55);
for (int i = 0; i < (int)strlen(keyword); i++)
{
keyword[i] = toupper(keyword[i]); //capitalize all of the character
}
for (int i = 0; i < total_book; i++)
{
substring = strstr(book[i].isbn, keyword);
if (substring)
{
cout << left << setw(15) << "ISBN" << setw(20) << "Author" << setw(35) << "Title"
<< setw(25) << "Publisher" << setw(7) << "Year" << setw(15) << "Genre"
<< setw(4) << "Qty" << setw(6) << "Price" << endl;
cout << "________________________________________________________________________________________________________________________________\n" << endl;
cout << left << setw(15) << book[i].isbn
<< setw(20) << book[i].author
<< setw(35) << book[i].title
<< setw(25) << book[i].publisher
<< setw(7) << book[i].year
<< setw(15) << book[i].genre
<< setw(4) << book[i].quantity
<< setw(6) << book[i].price << endl << endl
<< "(Located at shelf " << book[i].location.shelf << " level " << book[i].location.level << ")" << endl << endl;
cout << "________________________________________________________________________________________________________________________________" << endl;
}
else if (strstr(book[i].author, keyword) != 0)
{
cout << left << setw(15) << "ISBN" << setw(20) << "Author" << setw(35) << "Title"
<< setw(25) << "Publisher" << setw(7) << "Year" << setw(15) << "Genre"
<< setw(4) << "Qty" << setw(6) << "Price" << endl;
cout << "________________________________________________________________________________________________________________________________\n" << endl;
cout << left << setw(15) << book[i].isbn
<< setw(20) << book[i].author
<< setw(35) << book[i].title
<< setw(25) << book[i].publisher
<< setw(7) << book[i].year
<< setw(15) << book[i].genre
<< setw(4) << book[i].quantity
<< setw(6) << book[i].price << endl << endl
<< "(Located at shelf " << book[i].location.shelf << " level " << book[i].location.level << ")" << endl << endl;
cout << "________________________________________________________________________________________________________________________________" << endl;
}
}
system("pause");
}
Here is my book list I'm having in text file:
9781785785719,Yasha Levine,Surveillance Valley,Icon Books,2019,57.95,3,1,1,Political Science
9780241976630,John Maeda,How to Speak Machine,Portfolio UK,2019,89.95,2,1,1,Non-Fiction
9781119055808,Andre De Vries,R For Dummies,John Wiley,2015,107.77,4,1,2,Design
9780062018205,Dan Ariely,Predictably Irrational,Harper Collins,2010,39.90,2,1,1,Legal opinion
9780008327613,John Waish,The Globalist,Harper Collins,2019,109.90,2,1,1,Non-Fiction
9780525538349,John Doerr,Measure What Matters,Penguin LCC,2018,86.95,2,1,2,Management
9780807092156,Viktor Frankl,Man's Search for Meaning,Random House,2019,49.90,3,1,3,Biography
9780809875214,John Wick,The Assasinate of a Gang,Tree Production,2014,39.00,4,2,2,Fiction
9788373191723,J.R.R Tolkien,The Lord of the Rings,Allen & Unwin,1954,120.45,6,3,1,Fantasy Adventure
9783791535661,Lewis Carroll,Alice's Adventure in Wonderland,Macmillan,1865,100.25,5,3,2,Fantasy Fiction
9781517322977,Mikhail Bulgakov,The Master and Margarita,Penguin Books,1967,125.00,7,3,3,Romance Novel
9780676516197,Vladmir Nabokov,Lolita,Olympia Press,1955,98.25,3,3,1,Tragicomedy
9781095627242,Anna Sewell,Black Beauty,The Jarrold Group,1877,60.25,2,3,2,Fiction Novel
9788497592581,Umberto Eco,The Name of the Rose,Bompiani,1980,45.65,7,1,3,Historical Fiction
9780545790352,J.K.Rowling,Harry Potter and the Sorcerer's Stone,Arthur A. Levine,2015,44.87,1,3,1
I manage to only output until John Wick (8th line from text file) when user prompt "1" at the menu to show book list.
Related
How should I make my C++ code shorter without using arrays?
I'm currently working on an exercise given to me by my university professor and one of the questions is to create a program using data structures which inputs data of any topic and outputs them in a neat format. The problem is the question specifically states to not use arrays, and shorter codes grants bonus marks. How should I shorten my code below? #include <iostream> #include <iomanip> #include <string> using namespace std; struct musicRecord //structure name { //structure members string title; string artist; string album; string genre; string releaseYear; }; int main() { int numRecords; musicRecord music1, music2, music3, music4, music5; //structure variables //input cout << "==================================================="; cout << "\nRecord No. 1"<< endl; cout << "\nEnter the title : "; getline(cin, music1.title); cout << "\nEnter the artist's name : "; getline(cin, music1.artist); cout << "\nEnter the album name : "; getline(cin, music1.album); cout << "\nEnter the genre of the title : "; getline(cin, music1.genre); cout << "\nEnter the year the title was released : "; getline(cin, music1.releaseYear); cout << endl << "==================================================="; cout << "\nRecord No. 2" << endl; cout << "\nEnter the title : "; getline(cin, music2.title); cout << "\nEnter the artist's name : "; getline(cin, music2.artist); cout << "\nEnter the album name : "; getline(cin, music2.album); cout << "\nEnter the genre of the title : "; getline(cin, music2.genre); cout << "\nEnter the year the title was released : "; getline(cin, music2.releaseYear); cout << endl << "==================================================="; cout << "\nRecord No. 3" << endl; cout << "\nEnter the title : "; getline(cin, music3.title); cout << "\nEnter the artist's name : "; getline(cin, music3.artist); cout << "\nEnter the album name : "; getline(cin, music3.album); cout << "\nEnter the genre of the title : "; getline(cin, music3.genre); cout << "\nEnter the year the title was released : "; getline(cin, music3.releaseYear); cout << endl << "==================================================="; cout << "\nRecord No. 4" << endl; cout << "\nEnter the title : "; getline(cin, music4.title); cout << "\nEnter the artist's name : "; getline(cin, music4.artist); cout << "\nEnter the album name : "; getline(cin, music4.album); cout << "\nEnter the genre of the title : "; getline(cin, music4.genre); cout << "\nEnter the year the title was released : "; getline(cin, music4.releaseYear); cout << endl << "==================================================="; cout << "\nRecord No. 5" << endl; cout << "\nEnter the title : "; getline(cin, music5.title); cout << "\nEnter the artist's name : "; getline(cin, music5.artist); cout << "\nEnter the album name : "; getline(cin, music5.album); cout << "\nEnter the genre of the title : "; getline(cin, music5.genre); cout << "\nEnter the year the title was released : "; getline(cin, music5.releaseYear); //output cout << endl << "================================================================================================================"; cout << endl << "\t\t\t\t\t\tMUSIC RECORDS"; cout << endl << "================================================================================================================"; cout << endl << setw(5) << "Record No. |" << setw(10) << " Title" << setw(25) << "| Artist " << setw(20) << "| Album " << setw(20) << "| Genre " << setw(25) << "| Release Year |"; cout << endl << "----------------------------------------------------------------------------------------------------------------"; cout << endl << setw(12) << left << " 1"; cout << setw(27) << left << music1.title; cout << setw(21) << left << music1.artist; cout << setw(20) << left << music1.album; cout << setw(20) << left << music1.genre; cout << setw(15) << left << music1.releaseYear; cout << endl << "----------------------------------------------------------------------------------------------------------------"; cout << endl << setw(12) << left << " 2"; cout << setw(27) << left << music2.title; cout << setw(21) << left << music2.artist; cout << setw(20) << left << music2.album; cout << setw(20) << left << music2.genre; cout << setw(15) << left << music2.releaseYear; cout << endl << "----------------------------------------------------------------------------------------------------------------"; cout << endl << setw(12) << left << " 3"; cout << setw(27) << left << music3.title; cout << setw(21) << left << music3.artist; cout << setw(20) << left << music3.album; cout << setw(20) << left << music3.genre; cout << setw(15) << left << music3.releaseYear; cout << endl << "----------------------------------------------------------------------------------------------------------------"; cout << endl << setw(12) << left << " 4"; cout << setw(27) << left << music4.title; cout << setw(21) << left << music4.artist; cout << setw(20) << left << music4.album; cout << setw(20) << left << music4.genre; cout << setw(15) << left << music4.releaseYear; cout << endl << "----------------------------------------------------------------------------------------------------------------"; cout << endl << setw(12) << left << " 5"; cout << setw(27) << left << music5.title; cout << setw(21) << left << music5.artist; cout << setw(20) << left << music5.album; cout << setw(20) << left << music5.genre; cout << setw(15) << left << music5.releaseYear; cout << endl << "================================================================================================================"; cout << endl << "\t\t\t\t\tEND OF PROGRAM, THANK YOU"; cout << endl << "================================================================================================================"; return 0; }
As you might notice, you copy/paste code for each of your variables. So create function to avoid to duplicate code, for example: musicRecord inputMusicRecord(int recordNb) { musicRecord res; cout << "==================================================="; cout << "\nRecord No. " << recordNb << endl; cout << "\nEnter the title : "; getline(cin, res.title); cout << "\nEnter the artist's name : "; getline(cin, res.artist); cout << "\nEnter the album name : "; getline(cin, res.album); cout << "\nEnter the genre of the title : "; getline(cin, res.genre); cout << "\nEnter the year the title was released : "; getline(cin, res.releaseYear); return res; } void print(const musicRecord& music) { cout << endl << setw(12) << left << " 1"; cout << setw(27) << left << music.title; cout << setw(21) << left << music.artist; cout << setw(20) << left << music.album; cout << setw(20) << left << music.genre; cout << setw(15) << left << music.releaseYear; } And then, you might do: void print_header() { cout << endl << "================================================================================================================"; cout << endl << "\t\t\t\t\t\tMUSIC RECORDS"; cout << endl << "================================================================================================================"; cout << endl << setw(5) << "Record No. |" << setw(10) << " Title" << setw(25) << "| Artist " << setw(20) << "| Album " << setw(20) << "| Genre " << setw(25) << "| Release Year |"; } void print_separator() { cout << endl << "----------------------------------------------------------------------------------------------------------------"; } void print_footer() { cout << endl << "================================================================================================================"; cout << endl << "\t\t\t\t\tEND OF PROGRAM, THANK YOU"; cout << endl << "================================================================================================================"; } int main() { const musicRecord music1 = inputMusicRecord(1); const musicRecord music2 = inputMusicRecord(2); const musicRecord music3 = inputMusicRecord(3); const musicRecord music4 = inputMusicRecord(4); const musicRecord music5 = inputMusicRecord(5); print_header(); print_separator(); print(music1); print_separator(); print(music2); print_separator(); print(music3); print_separator(); print(music4); print_separator(); print(music5); print_footer(); }
Searching for specific position of binary file to start reading or writing
Lets say you have a data file with multiple entries of struct type data Based on the code number variable of one of the data entries,you are supposed to draw out the remaining variables of the structure. Or maybe should I try to find the record number based on the scode? then using the record number to find the size I need to skip from the beginning using seekg char scode[MAX]; Subject M; afile.open (fileName, ios::in | ios::out | ios::binary); cout << "Enter Subject code: "; cin >> scode; cin.clear(); cin.ignore(100,'\n'); cout << endl << endl; while (afile.read (reinterpret_cast <char *>(&M), sizeof (M))) { if (strcmp(scode,M.subCode) == 1) { cout << "Subject code not found" << endl; cout << "----------------------------" << endl; return; } else { afile.seekg(-strlen(M.subCode),ios::cur); cout << "Subject Code: " << M.subCode << endl << "Subject Name: " << M.subTitle << endl; cout << left << setw(8) << "Task" << left << setw(14) << "Title" << right << setw(7) << "Weight" << right << setw(7) << "Upon" << right << setw(7) << "Mark" << right << setw(12) << "Obtained" << endl << endl; cout << setw (66) << setfill ('-') << "-" << endl; cout << setfill (' '); while (afile.read (reinterpret_cast <char *>(&M), sizeof (M))) { for (int i = 1; i <= M.noTask; i++) { cout << left << setw(8) << i << left << setw(14) << M.Task[i].title << right << setw(7) << M.Task[i].weight << right << setw(7) << M.Task[i].fullmark << right << setw(7) << M.Task[i].mark << right << setw(12) << M.Task[i].mark / M.Task[i].fullmark * M.Task[i].weight << endl; } } }
Must have pointer to object type c++ (Array)
I have a pointer to object error type for the variable "carrierTime" i have created. If i make this an array, carrierTime becomes an error in the first if statement, however if i leave it without any array i get an error on the last line of the code where i have used carrierTime in a multiplication. can anyone help?? platform used:visual studios #include "AMcore.h" #include <iostream> #include <iomanip> #include <fstream> #include <string> using namespace std; int main() { cout << "Amplitude Modulation Coursework" << endl; cout << "Name: Mohammad Faizan Shah" << endl; cout << "Student ID: 5526734 \n\n\n" << endl; std::ifstream file,file2; string filename1,filename2; int rowCounter = 0; double informationTime; double informationAmplitudeAmount[361]; long double carrierTime; double carrierAmplitudeAmount[361]; double totalAmplitudeAmount[1000]; int plotPoint; cout << "Please enter the filename of the Carrier wave \n" << endl; cin >> filename1; file.open("carrier.txt"); if (file.is_open()) { file >> carrierTime; while (!file.fail()) { cout << "row" << setw(3) << rowCounter; cout << " Time = " << setw(5) << carrierTime; file >> carrierAmplitudeAmount[rowCounter]; rowCounter++; if (!file.fail()) { cout << " Carrier signal= " << setw(5) << carrierAmplitudeAmount; file >> carrierTime; } cout << endl; } if (file.eof()) cout << "Reached the end of file marker" << endl; else cout << "Error whilst reading input file" << endl; } else { cout << "Error opening input file, "; cout << "check carrier.txt exists in the current directory." << endl; } file.close(); cout << "\n\n" << endl; cout << "Please enter the filename of the information wave \n\n\n" << endl; cin >> filename2; file2.open("information.txt"); if (file2.is_open()) { file2 >> informationTime; while (!file2.fail()) { cout << "row" << setw(3) << rowCounter; cout << " Time = " << setw(5) << informationTime; file2 >> informationAmplitudeAmount[361]; rowCounter++; if (!file2.fail()) { cout << " Carrier signal= " << setw(5) << informationAmplitudeAmount; file2 >> informationTime; } cout << endl; } if (file2.eof()) cout << "Reached the end of file marker" << endl; else cout << "Error whilst reading input file" << endl; } else { cout << "Error opening input file, "; cout << "check carrier.txt exists in the current directory." << endl; } file.close(); cout << "Reading from txt file has completed" << endl << endl; cout << "\n\n" << endl; cout << "\n\n" << endl; cout << "please enter number of sample points to plot:| \n" << endl; do{ cin >> plotPoint; if (plotPoint <= 361) { cout << "\n plotting the graph.\n" << endl; } else if (plotPoint > 361) { cout << "Value is too high.. Try value lower than 361\n" << endl; } } while (plotPoint > 361); cout << "row" << setw(3) << rowCounter; file >> carrierAmplitudeAmount[361]; rowCounter++; plotPoint = 361 / plotPoint; cout << " Time \| Amplitude Modulation plot\n------------+--------------------------------------------------\n"; totalAmplitudeAmount[0] = carrierAmplitudeAmount[0] * informationAmplitudeAmount[0]; cout << setw(6) << carrierTime << setw(4) << "\|" << setw(48) << "*" << totalAmplitudeAmount[0] << endl; for (int i = 1; i <= 361; i = i + plotPoint) { totalAmplitudeAmount[i] = informationAmplitudeAmount[i] * carrierAmplitudeAmount[i]; int y = totalAmplitudeAmount[i] * 22; cout << setw(6) << carrierTime[i++] << setw(4) << "\|" << setw(26 + y) << "*" << totalAmplitudeAmount[i] << endl; } cout << "End of program" << endl; system("pause"); return 0; }
cout << setw(6) << carrierTime[i++] << setw(4) << "\|" << setw(26 + y) << "*" << totalAmplitudeAmount[i] << endl; carrierTime[i++] does not look correct. The variable is not defined as a pointer. Also, proper debugging would help you catch these errors for yourself.
C++: Append string to output of first and last array index in loop
I tried to word the title as best as I could. Here I have a function that indexes through two parallel arrays and then outputs them with some formatting. void outputTable(string salsa_jars[], int jars_sold[], int index[]) { int totalSold = 0; cout << setw(8) << "\nSalsa type sells: " << endl << "-------------------------------" << endl; for(int i = 0; i <= (SALSA_TYPES-1); i++) { totalSold += jars_sold[index[i]]; cout << setw(15) << left << salsa_jars[index[i]] << setw(15) << right << jars_sold[index[i]] << endl; } cout << "-------------------------------" << endl << "Total sales: " << setw(17) << totalSold << endl; } What I'm trying to achieve is to add a string to the first and last outputs of the array. Below is my attempt. void outputTable(string salsa_jars[], int jars_sold[], int index[]) { int totalSold = 0; cout << setw(8) << "\nSalsa type sells: " << endl << "-------------------------------" << endl; for(int i=0;i<=(SALSA_TYPES-1);i++) { if(i == 0){ cout << setw(7) << left << salsa_jars[index[i]] << "(Highest)" << setw(14) << right << jars_sold[index[i]] << endl; } else if (i == (SALSA_TYPES-1)){ cout << setw(7) << left << salsa_jars[index[i]] << "(Lowest)" << setw(15) << right << jars_sold[index[i]] << endl; } else{ totalSold += jars_sold[index[i]]; cout << setw(15) << left << salsa_jars[index[i]] << setw(15) << right << jars_sold[index[i]] << endl; } } cout << "-------------------------------" << endl << "Total sales: " << setw(17) << totalSold << endl; } But the code seems redundant, and I couldn't think of any other way to do it. If anyone has any pointers, I would appreciate it. Thanks.
Just prepare appropriate title for entry, and use common logic for displaying it: for(int i=0;i<=(SALSA_TYPES-1);i++) { string title = toString(salsa_jars[index[i]]); if(i == 0){ title += " (Highest)"; } else if (i == (SALSA_TYPES-1)){ title += " (Lowest)"; } totalSold += jars_sold[index[i]]; cout << setw(15) << left << title << setw(15) << right << jars_sold[index[i]] << endl; } }
How would I go about outputting the output from ALL of the above code?
Basically I want an exact copy of the code that appears in the console window to also be outputted to a txt file.. #include <conio.h> #include <iostream> #include <dos.h> #include <stdlib.h> #include <windows.h> #include <stdio.h> #include <fstream> using namespace std; //Initialising gotoxy Comand void gotoxy(int col, int row) { COORD coord; coord.X = col; coord.Y = row; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } int main() { char name1[20], name2[20], name3[30], name4[20]; int funcNum = 0; int n1Nv, n1Mv, n1SEv, n1SWv; int n2Nv, n2Mv, n2SEv, n2SWv; int n3Nv, n3Mv, n3SEv, n3SWv; int n4Nv, n4Mv, n4SEv, n4SWv; int n1Total, n2Total, n3Total, n4Total, perTotal; double n1Per, n1PerTotal; double n2Per, n2PerTotal; double n3Per, n3PerTotal; double n4Per, n4PerTotal; double maxVote; //Introduction cout << "================================================================================"; cout << " Ballot Results" << endl; cout << " Version 2.1" << endl; cout << " Created by Team b0nkaz" << endl; cout << "================================================================================" << endl; //Candidate Identification cout << "Enter the candidates running for president" << endl << endl; //cin.getline (workaround,30); //** cout << "Candidate One: "; cin.getline (name1,20); cout << "Candidate Two: "; cin.getline (name2,20); cout << "Candidate Three: "; cin.getline (name3,20); cout << "Candidate Four: "; cin.getline (name4,20); cout << " " << endl; //Separator cout << "--------------------------------------------------------------------------------" << endl; cout << "Input vote numbers from each region pressing enter after each input:" << endl << endl; //Separator cout << "--------------------------------------------------------------------------------" << endl; //Input Table //Regions gotoxy(22,19); cout << "North" << endl; gotoxy(31,19); cout << "Midlands" << endl; gotoxy(43,19); cout << "South East" << endl; gotoxy(57,19); cout << "South West" << endl; gotoxy(69,19); cout << "| Total" << endl; cout << "_____________________________________________________________________|__________" << endl; gotoxy(69,21); cout << "|"; gotoxy(69,22); cout << "|"; gotoxy(69,23); cout << "|"; gotoxy(69,24); cout << "|"; gotoxy(69,25); cout << "|"; gotoxy(69,25); cout << "|"; gotoxy(69,26); cout << "|"; gotoxy(69,27); cout << "|"; gotoxy(69,28); cout << "|"; gotoxy(69,29); cout << "|"; //Candidates gotoxy(0,22); cout << name1; gotoxy(0,24); cout << name2; gotoxy(0,26); cout << name3; gotoxy(0,28); cout << name4; //Equals cout << endl; cout << "_____________________________________________________________________|__________" << endl; //Vote Input //North gotoxy(22,22); cin >> n1Nv; gotoxy(22,24); cin >> n2Nv; gotoxy(22,26); cin >> n3Nv; gotoxy(22,28); cin >> n4Nv; //Midlands gotoxy(31,22); cin >> n1Mv; gotoxy(31,24); cin >> n2Mv; gotoxy(31,26); cin >> n3Mv; gotoxy(31,28); cin >> n4Mv; //South East gotoxy(43,22); cin >> n1SEv; gotoxy(43,24); cin >> n2SEv; gotoxy(43,26); cin >> n3SEv; gotoxy(43,28); cin >> n4SEv; //South West gotoxy(57,22); cin >> n1SWv; gotoxy(57,24); cin >> n2SWv; gotoxy(57,26); cin >> n3SWv; gotoxy(57,28); cin >> n4SWv; //Total Votes //Name1 gotoxy(72,22); n1Total = n1Nv + n1Mv + n1SEv + n1SWv; cout << n1Total; //Name2 gotoxy(72,24); n2Total = n2Nv + n2Mv + n2SEv + n2SWv; cout << n2Total; //Name3 gotoxy(72,26); n3Total = n3Nv + n3Mv + n3SEv + n3SWv; cout << n3Total; //Name4 gotoxy(72,28); n4Total = n4Nv + n4Mv + n4SEv + n4SWv; cout << n4Total << endl << endl << endl; //Percentage Calculation perTotal = n1Total + n2Total + n3Total + n4Total; //Candidate One n1Per = n1Total*100; n1PerTotal = n1Per/perTotal; //Candidate Two n2Per = n2Total*100; n2PerTotal = n2Per/perTotal; //Candidate Three n3Per = n3Total*100; n3PerTotal = n3Per/perTotal; //Candidate Four n4Per = n4Total*100; n4PerTotal = n4Per/perTotal; cout << "Please wait for calculation..." << endl << endl; //Spinning Loading Line //std::cout << '-' << std::flush; //for(;;) //{ //Sleep(100); //std::cout << "\b\\" << std::flush; //Sleep(100); //std::cout << "\b|" << std::flush; //Sleep(100); //std::cout << "\b/" << std::flush; //Sleep(100); //std::cout << "\b-" << std::flush; //} //Sleeping Program Sleep(1500); //1.5 secs //Total Output cout << "Candidate percentage:" << endl << endl; //Converting To One Decimal Place cout << fixed; std::cout.precision(1); //Vote Percentages cout << name1 << " = " << n1PerTotal << "%" << endl; cout << name2 << " = " << n2PerTotal << "%" << endl; cout << name3 << " = " << n3PerTotal << "%" << endl; cout << name4 << " = " << n4PerTotal << "%" << endl << endl;; //Calculating Winnner maxVote=n1PerTotal; if (n2PerTotal>maxVote) maxVote=n2PerTotal; if (n3PerTotal>maxVote) maxVote=n3PerTotal; if (n4PerTotal>maxVote) maxVote=n4PerTotal; //Separator cout << "--------------------------------------------------------------------------------" << endl; //Sleeping Program Sleep(1500); //1.5 secs if(maxVote==n1PerTotal) { cout << " ***********************************************************************" << endl; cout << " " << name1 << " " << endl; cout << " is the new president of The British Society of IT Professionals " << endl; cout << " with " << n1PerTotal << "% of the vote " << endl; cout << " ***********************************************************************" << endl << endl; //Separator cout << "--------------------------------------------------------------------------------" << endl; } else if(maxVote==n2PerTotal) { cout << " ***********************************************************************" << endl; cout << " " << name2 << " " << endl; cout << " is the new president of The British Society of IT Professionals " << endl; cout << " with " << n2PerTotal << "% of the vote " << endl; cout << " ***********************************************************************" << endl << endl; //Separator cout << "--------------------------------------------------------------------------------" << endl; } else if(maxVote==n3PerTotal) { cout << " ***********************************************************************" << endl; cout << " " << name3 << " " << endl; cout << " is the new president of The British Society of IT Professionals " << endl; cout << " with " << n3PerTotal << "% of the vote " << endl; cout << " ***********************************************************************" << endl << endl; //Separator cout << "--------------------------------------------------------------------------------" << endl; } else if(maxVote==n4PerTotal) { cout << " ***********************************************************************" << endl; cout << " " << name4 << " " << endl; cout << " is the new president of The British Society of IT Professionals " << endl; cout << " with " << n4PerTotal << "% of the vote " << endl; cout << " ***********************************************************************" << endl << endl;; //Separator cout << "--------------------------------------------------------------------------------" << endl; } cout << "Press any key to exit..." << endl; getch(); //system("pause"); } I am using MS Visual Studio 2010 and any help would be great! Please note I am still very new to C++. EDIT I would like to be able to see the output in CMD as well as have a separate txt file with the same output code. Everything that is displayed in the CMD window also copied into the txt file.
use an ofstream operator as such. For some reason I can't seem to find the comment button or I would be involved in the discussion above... but I've edited my code below to reflect what seem to be some concerns of yours. #include<fstream> using namespace std; int main() { ofstream fout; fout.open("data.txt"); //Will create a new file if one is not already in existence //You can put a static filename here or have them enter a string //If you use a custom string your input will be "fout.open(STRING.c_str()); fout<<"Used exactly like cout from this point on"; fout.close(); //When you are done using it to close the file } I chose to name my ofstream operator fout because (while this is not always good to do) this way you can quickly change all of your cout's to fout's, or replicate them. If the user enters a value and you want to spit it back out as well, you can use the ofstream operator after every cin. You can find more information about ofstream here... Hope this helped!
You can do this from outside of the application using tee. tee takes the output of a program and splits it into two streams (e.g. one to stdout and one to a file). Unfortunately, Windows doesn't come with a tee command but there are many implementations available. If you want this to happen from within your program you can do the equivalent of tee using a custom ofstream. This article explains how.