I'm doing a fibonacci and I want to save all the numbers in a .txt, i have done the code but when the cicles gets big values the program express it as an exponential, and then gets bigger as .INF.
How I can save the entry number?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ofstream File;
File.open("fibo.txt");
double val0 = 0, val1 = 1, i = 0, n, out;
cout << "Enter n of Fibonacci(n): ";
cin >> n;
while (i < n) {
if (n == 0) {
File << i << "- " << 0 << endl;
i++;
}
else {
out = val0 + val1;
val0 = val1;
val1 = out;
i++;
File << i << "- " << val0 << endl;
}
}
File.close();
return 0;
}
You can use boost::multiprecision, it's very intuitive and easy to use. Your code can be modified by changing (only) a couple of lines:
#include <boost/multiprecision/cpp_int.hpp> // need this
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using namespace boost::multiprecision;
int main()
{
ofstream File;
File.open("fibo.txt");
cpp_int val0 = 0, val1 = 1, out; // arbitrary precision integers
int i = 0, n;
cout << "Enter n of Fibonacci(n): ";
cin >> n;
while (i < n) {
if (n == 0) {
File << i << "- " << 0 << endl;
i++;
}
else {
out = val0 + val1;
val0 = val1;
val1 = out;
i++;
File << i << "- " << val0 << endl;
}
}
File.close();
return 0;
}
Related
How do I make my code specifically on else if to print out this if my input is 45?
01.02.03.04.05.06.07.08.09.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
#include <iostream>
#include <string>
using namespace std;
int main() {
string dot = "";
int x;
cin >> x;
if (x<=10){
for (int n=1; n<=x; n++){
cout << dot << n ;
dot =".";
}
}
else if(x>10&&x<=100) {
for (int i = 1; i <=x; ++i){
for (int j = 1; j <=10; ++j){
cout << dot << x;
dot=".";
}
cout << endl;
}
}
else{
cout << "OUT OF RANGE";
}
return 0;
}
Your entire program can be simplified using setw and setfill to do the hard work for you of inserting leading zero chars where needed. #include <iomanip> to have access to these stream modification functions.
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int x;
cin >> x;
for (int i = 1; i <= x; i++)
{
cout << setw(2) << setfill('0') << i;
char delimiter = ((i % 10) && (i != x)) ? '.' : '\n';
cout << delimiter;
}
cout << endl;
}
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int x = 0;
cin >> x;
if (x > 100)
{
cout << "Out of range." << endl;
return 0;
}
for (int i = 1; i <= x; i++)
{
cout << setfill('0') << setw(2) << i << ((i % 10) ? "." : "\n");
}
return 0;
}
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main() {
bool done = false;
cout << setprecision(20) << endl;
ifstream infile("in.txt");
ifstream outfile("out.txt");
while(!infile.eof()) {
int sign = 1;
double pi = 0;
long n;
infile >> n;
for(long i = 1; i < n; i += 2) {
pi += sign/(double)i;
sign = -sign;
}
pi *= 4;
cout << "value of pi for n = " << n << " is " << pi << endl;
}
return 0;
}
This reads from a file and prints to the console but I can't get the code to print to the outfile. I've tried doing
outfile<< "value of pi for n = " << n << " is " << pi << endl;
But that doesn't seem to work
It does not write to the file because you defined outfile as an std::ifstream
ifstream is for input files.
Define it as an ofstream and it should work.
Example:
ofstream outfile("out.txt");
I am trying to get some values line by line from a text file:
17.09 284.60 486.01 34.12 12.04 1.20 2.33 36.85 73.44
31.25 196.09 323.26 69.76 47.33 79.82 11.42 27.97 66.61
28.76 41.45 992.29 1.29 42.33 10.83 19.16 5.86 1.88
Taking these values and putting it into a vector. Each row has values to be used in a calculation.
My code:
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <vector>
using namespace std;
int main() {
ifstream xfile;
string input;
double num=0;
int count = 0;
vector <double> myvector;
cout << "Input the file: ";
cin >> input;
xfile.open(input);
if (xfile.is_open()) {
cout << "File accessed!" << endl;
while (getline(xfile, input)) {
count++;
myvector.push_back(num);
}
}
else {
cout << "File opening failed!"<<endl;
}
cout << "Numbers of lines in the file : " << count << endl;
for (int i = 0; i < myvector.size(); i++) {
cout << myvector[i] << "\t";
}
cin.fail();
return 0;
}
My output is somewhat correct, only that it is printing out just zeroes:
https://ibb.co/xqwT1hR
EDIT: the input is for the name of file. "ahu_long.txt"
You never used your num variable.
double num=0;
....
....
size_t pos = 0;
std::string token;
while (getline(xfile, input)) {
count++;
// you need convert your "input" to a double and save it to "num"
while ((pos = input.find(" ")) != std::string::npos) {
token = input.substr(0, pos);
// std::cout << token << std::endl;
num = atof(token.c_str());
myvector.push_back(num);
input.erase(0, pos + delimiter.length());
}
}
Change your variable with what you read from the file.
I am trying to get the factors of positive integer. What I want is 8 = 2*2*2. However, what I get is *2*2*2. How can I get ride of the first *? Is there a standard way to better describe this situation?
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int num, i = 2;
const char separator = '*';
cout << "Input a positive integer: ";
cin >> num;
while(num !=1){
while((num % i) != 0){
i++;
}
cout << setw(2) << setfill(separator) << i;
num = num/i;
}
}
Input a positive integer: 8
*2*2*2
Use a separator that is updated. Start with "" and set to "*" thereafter.
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int num, i = 2;
const char *separator = "";
cout << "Input a positive integer: ";
cin >> num;
do {
while((num % i) != 0){
i++;
}
cout << separator << i;
separator = "*";
num = num/i;
} while (num > 1);
}
Also changed to do loop to cope with num == 1 and num == 0 which print nothing in OP's original code. Code could use unsigned as a further extension/ protection.
One way to accomplish that is by outputting setfill(separator) only when num is not equals to 1.
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int num, i = 2;
const char separator = '*';
cout << "Input a positive integer: ";
cin >> num;
while(num !=1){
while((num % i) != 0){
i++;
}
cout << setw(2) << i;
num = num/i;
if (num != 1)
cout << setfill(separator);
}
}
I hope someone can help me with this issues. I am creating a HTML lexical analyzer in c++. According to the teacher I am supposed to have 3 files. one header and 2 main .cpp and it should be able to read a file
This is my file try.txt
<<<<<Hello there <H1 style=”BOLD”>header!!</H1>
<<
<< =
This is my header
#ifndef tokens_h
#define tokens_h
#include <string>
#include <iostream>
enum tokens {TEXT, LANGLE = 60, RANGLE = 62, SLASH = 47, ID, EQ = 61, QSTRING = 34, OTHER, END};
/* TEXT = 0
LANGLE = 60
RANGLE = 62
SLASH = 47
ID = 48
EQ = 61
QSTRING = 34
OTHER = 36
END = 36
*/
int getToken(std::istream *br, std::string a);
#endif
This is my main.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include "tokens.h"
using namespace std;
int main(int argc, char *argv[])
{
//defineTokens();
istream *br;
ifstream infile;
string output;
int a;
vector<int> count;
int langle = 0;
string line;
if(argc == 1){
while(cin.good() ){ //Get continous input
br = &cin;
getline(cin,line);
getToken(br,line);
}
}
else if(argc != 2){
return 1;
}else{
infile.open(argv[1]);
if( infile.is_open()){
br = &infile;
while(!infile.eof()){
getline(infile,output);
getToken(br,output);
}
}
else{
cout << argv[1] << "Can't Be Opened" << endl;
return 1;
}
}
}
and this is my tokens.cpp where I print the results
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <utility>
#include "tokens.h"
using namespace std;
void compar(int ch)
{
vector<int> text;
vector<int> langle;
//string langle;
vector<int> rangle;
vector<int> slash;
vector<int> id;
vector<int> eq;
vector<int> qstring;
vector<int> other;
map <string, int> result;
int c=0;
int d=0;
int sum;
string r;
switch(ch){
case 60:static int countlangle = 0;
countlangle ++;
result["LANGLE"]= countlangle;
cout << "LANGLE: " << result["LANGLE"] << " ";
break;
case 62:static int countrangle = 0;
countrangle ++;
result["RANGLE"]= countrangle;
cout << "RANGLE: " << result["RANGLE"] << " ";
break;
case 47:static int countslash = 0;
countslash ++;
result["SLASH"]= countslash;
cout << "SLASH: " << result["SLASH"] << " ";
break;
case 61:static int counteq = 0;
counteq ++;
result["EQ"]= counteq;
cout << "EQ: " << result["EQ"] << " ";
break;
case 34:static int countqstring = 0;
countqstring ++;
result["QSTRING"]= countqstring;
cout << "QSTRING: " << result["QSTRING"] << " ";
break;
}
}
int getToken(istream *br, string a)
{
int b;
string d = "no";
string f = "no";
string r;
vector<char> st;
vector<string> trial;
vector<int> countr;
vector<int> countl;
vector<char> quotes;
string ans;
int x=0;
r = a;
cout << a[27];
int found;
found = a.find('\"');
cout << found<<"XXxxxxxX";
for(int i = 0; i< a.length();i++){ //read entire string
if(a[i] == '<'){
// cout << LANGLE << " ";
d="yes";
x +=1;
countr.push_back(LANGLE);
//cout << count.size();
//cout << x;
compar(LANGLE);
b =LANGLE;
// return LANGLE;
}
else if(a[i]== '>' && d == "yes"){
f = "yes";
b = RANGLE; //assing to the int variable the value from the enum header
compar(RANGLE);
}
else if(a[i]== '/' && d == "yes"){
compar(SLASH);
}
else if(a[i] == '=' && d == "yes"){
compar(EQ);
}
else if(a[found] == '\"' && d == "yes"){
// for(int k =0;k < quotes.size();k++)
//cout << r[found] <<"XXX";
compar(QSTRING);
}
}
return 0;
}
The program reads <>= without a problem but when I try to read a '\"' with cout << a[27];
I get this: ?
if I print cout << a;
i get <<<<<Hello there <H1 style=”BOLD”>header!!</H1> // this is the string I am trying to read
when I use found = a.find('\"'); it gives me a -1
My question is why my program cannot recognized quotes? is it the way I am reading the file?
thanks in advance
Your file contains:
”
whereas your lexer looks for:
"
These are distinct.