Zapis wylosowanych liczb do pliku.
Ostatnio zmodyfikowano 2017-06-05 19:00
rikoooo1234 Temat założony przez niniejszego użytkownika |
Zapis wylosowanych liczb do pliku. » 2017-06-04 22:49:41 Witam. Jestem początkujący, jeśli chodzi o c++, ogarnąłem już sobie jak wylosować liczby, lecz chciałem je teraz zapisać do pliku txt. Przeszukiwałem wiele tematów, próbowałem coś zdziałać, ale jak krew w piach. Proszę o pomoc.
#include <iostream> #include <cstdlib> #include <time.h> #include <stdio.h> #include<windows.h>
using namespace std;
int liczba;
int main() { cout<< "Losuje 6 liczb od 1 do 49 i zapisuje do pliku" << endl;
srand(time(NULL));
for(int i=1;i<=6;i++) {
liczba=rand()%49+1; cout <<liczba<<endl; }
}
getchar();getchar(); return 0; }
|
|
Saran |
» 2017-06-04 23:06:18 |
|
carlosmay |
» 2017-06-05 01:04:52 #include <iostream> #include <fstream> #include <random> #include <set>
int main() { std::mt19937 gen( std::random_device { }() ); std::uniform_int_distribution <> dist( 0, 49 ); std::size_t nums_size { 6 }; std::set < int > nums; while( nums.size() < nums_size ) { nums.insert( dist( gen ) ); } std::ofstream fout( "test.txt" ); if( fout ) { for( auto const & el: nums ) { fout << el << '\n'; } } else { } } std::uniform_int_distribution<>std::set<>range_for |
|
mokrowski |
» 2017-06-05 10:56:49 Wiadomo.... co kto lubi. Bez kontenera można tak: #include <iostream> #include <algorithm> #include <fstream> #include <random> #include <iterator> #include <sstream> #include <string> #include <cstdlib>
int main() { std::ofstream fout( "text.txt" ); if( !fout ) { std::cerr << "Problem z otwarciem pliku mój Padawanie!\n"; return EXIT_FAILURE; } std::mt19937_64 gen( std::random_device { }() ); std::uniform_int_distribution <> dist( 0, 49 ); std::generate_n( std::ostream_iterator < std::string >( fout, "\n" ), 6, [ & ]() { return std::to_string( dist( gen ) ); } ); return EXIT_SUCCESS; }
|
|
rikoooo1234 Temat założony przez niniejszego użytkownika |
» 2017-06-05 18:31:15 // Copyright (C) 2007-2014 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the // Free Software Foundation; either version 3, or (at your option) // any later version.
// This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional // permissions described in the GCC Runtime Library Exception, version // 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and // a copy of the GCC Runtime Library Exception along with this program; // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see // <http://www.gnu.org/licenses/>.
/** @file bits/c++0x_warning.h * This is an internal header file, included by other library headers. * Do not attempt to use it directly. @headername{iosfwd} */
#ifndef _CXX0X_WARNING_H #define _CXX0X_WARNING_H 1
#if __cplusplus < 201103L #error This file requires compiler and library support for the \ ISO C++ 2011 standard. This support is currently experimental, and must be \ enabled with the -std=c++11 or -std=gnu++11 compiler options. #endif
#endif
|
|
garlonicon |
» 2017-06-05 19:00:26 -std=c++11 or -std=gnu++11 compiler options |
Włącz obsługę C++11. |
|
« 1 » |