Writing to a csv file
Apr 5, 2016 at 11:04pm UTC
Hey guys, I've been trying to save user input to an excel .csv file, but I can't seem to get it right, nothing is printed, also, how can I make it so it writes Events in different columns and Time in different cells and save that to the file? Could you please help me? Thank you!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include<string>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <vector>
std::vector<std::string> Input(100);
int x = 1;
bool isOpen = true ;
int main(){
std::cout << "Event" ;
std::cin >> Input[x];
std::ofstream myfile;
myfile.open("tryy.csv" );
myfile << Input[x];
x++;
std::cout << "Time" << std::endl;
int t;
std::cin >> t;
myfile << t;
myfile.close();
return 0;
}
Apr 5, 2016 at 11:46pm UTC
To give you an idea, look at lines 38 to 50:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#include <iostream>
#include <iomanip>
#include <fstream>
#include <chrono>
#include <cctype> // For toupper function.
void setMultiplicationTable(int [][501]);
void findProduct(int &, int &);
void validateArrayBounds(int &);
int main()
{
int table[501][501];
int firstNum{ 0 }, secondNum{ 0 };
char tryAgain{ ' ' };
setMultiplicationTable(table);
do
{
findProduct(firstNum, secondNum);
std::cout << "The product is: " << table[firstNum][secondNum] << std::endl;
std::cout << "Would you like to try again?(Y/N)?" ;
std::cin >> tryAgain;
} while (toupper(tryAgain) == 'Y' );
return 0;
}
void setMultiplicationTable(int multTbl[][501])
{
std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now(); // Start the timer
std::fstream outPutFile;
outPutFile.open("Multiplication Table.csv" , std::ios::out);
for (int countRows = 0; countRows < 501; countRows++)
{
for (int countCols = 0; countCols < 501; countCols++)
{
multTbl[countRows][countCols] = countRows * countCols;
outPutFile << std::setw(6) << multTbl[countRows][countCols] << "," ;
}
outPutFile << std::endl;
}
outPutFile.close();
std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now(); // Determine the timer after execution.
std::chrono::duration<double > time_span = std::chrono::duration_cast<std::chrono::duration<double >>(t2 - t1); // Substract the timer after execution - timer started.
std::cout << "Completed Multiplication Table in " << time_span.count() << " seconds." << std::endl; // Notify the user.
}
void findProduct(int & num1, int & num2)
{
std::cout << "Enter the first Number: " ;
validateArrayBounds(num1);
std::cout << "Enter the second Number: " ;
validateArrayBounds(num2);
}
void validateArrayBounds(int & input)
{
std::cin >> input;
while (input < 0 || input > 500)
{
std::cout << "The min/max input for the "
<< "multiplication table is 0 - 500\n" ;
std::cout << "Please, try again.\n" ;
std::cout << ">>>" ;
std::cin >> input;
}
}
Topic archived. No new replies allowed.