Přidat otázku mezi oblíbenéZasílat nové odpovědi e-mailem Náhodný výběr slov (Jazyk C)

Zdravím... začal jsem programovat v jazyku C a došel jsem do fáze, kdy nemůžu přijít na to, jak napsat program pro náhodný výběr například z 5 slov. Přemýšlel jsem , že bych ty slova uložil do stringu a z nich bych nějak 1 náhodně vybral. Mohl by mi prosím někdo poradit , jak na to? Děkuji moc.

Předmět Autor Datum
Pro jednoduchost můžeš mít např. dvourozměrné pole, kde budeš mít řetězce a pak si nechat generovat…
Niko Bellic 05.02.2013 23:36
Niko Bellic
Tak si ty slova ulož do pole stringů a jenom náhodně generuj index do tohoto pole...
MaSo 05.02.2013 23:37
MaSo
Dobře, díky vám oběma ;-) vyzkouším to...
jindraa 05.02.2013 23:43
jindraa
Narazil jsem na problém... int nahodne; char *str[3] = {"prvni","druhe","treti"}; nahodne = rand()…
jindraa 06.02.2013 00:20
jindraa
Dej: int nahodne; string str[3] = {"prvni","druhe","treti"}; nahodne = (rand() % 3); this->label1->…
Flash_Gordon 06.02.2013 09:54
Flash_Gordon
To mně nechce vzít string. Vyhodí to error: 'string' : undeclared identifier
jindraa 06.02.2013 10:13
jindraa
]:( Nemáš v projektu includovanou knihovnu string.h // EDIT Teda kecám. namísto string použij std…
Flash_Gordon 06.02.2013 10:16
Flash_Gordon
Pořád mi to nechce pobrat :-( teď to hodilo error: string : is not a member of 'std' i ten error co…
jindraa 06.02.2013 10:35
jindraa
// Edit Vlastně dnes je to #include <string> nikoliv zastaralý #include <string.h>. Pak použij usi…
Flash_Gordon 06.02.2013 10:38
Flash_Gordon
#pragma once #include "stdafx.h" #include "stdio.h" #include "math.h" #include "stdlib.h" #include "…
jindraa 06.02.2013 10:52
jindraa
No to nebude fungovat. Protože vymezuješ uvnitř bloku nahodnaslova: using namespace System; using n…
Flash_Gordon 06.02.2013 11:01
Flash_Gordon
Pořád to samý. Zkoušel jsem i ten string bez std a nic...Nejspíš to nechám být :-/ Ještě doplním ,…
jindraa 06.02.2013 11:18
jindraa
#pragma once #include "stdafx.h" #include "stdio.h" #include "math.h" #include "stdlib.h" #include "…
Flash_Gordon 06.02.2013 11:41
Flash_Gordon
Hezky, díky moc;-) teď už to vyhodilo jenom 1 error na řádku this->label1->Text = str[nahodne]; '…
jindraa 06.02.2013 11:55
jindraa
Fffff. namísto: this->label1->Text = str[nahodne]; umísti using namespace System; String^ str2 =…
Flash_Gordon 06.02.2013 12:20
Flash_Gordon
super, jseš profík! Díky moc za trpělivost, už to běží ;-) ani nevíš, jakou si mi udělal radost ... poslední
jindraa 06.02.2013 12:25
jindraa
dobře... díky, vyzkouším ;)
jindraa 06.02.2013 10:52
jindraa

Narazil jsem na problém...

int nahodne;
char *str[3] = {"prvni","druhe","treti"};
nahodne = rand() % 2+1;
this->label1->Text = System::Convert::ToString(str[nahodne]);

Když to takhle napíšu, tak se mi po stisku tlačítka zobrazí místo toho náhodného slova pouze: True.

#pragma once

#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#include "stdlib.h"
#include "string.h"


namespace nahodnaslova {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;
	protected: 
	private: System::Windows::Forms::Label^  label1;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(378, 157);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 0;
			this->button1->Text = L"button1";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(379, 122);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(35, 13);
			this->label1->TabIndex = 1;
			this->label1->Text = L"label1";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(764, 326);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {


int nahodne;
std::string str[3] = {"prvni","druhe","treti"};
nahodne = (rand() % 3);
this->label1->Text = str[nahodne];

			 }
	};
}

Tady je výpis chyb:

'string' : is not a member of 'std'
'string' : undeclared identifier
'missing ';' before identifier 'str'
'str' : undeclared identifier
syntax error : '{'
missing ';' before '{'
syntax error : missing ';' before '}'
'rand': identifier not found
'str' : undeclared identifier

Díky za trpělivost ;-)

Pořád to samý. Zkoušel jsem i ten string bez std a nic...Nejspíš to nechám být :-/

Ještě doplním , že podle toho původního kódu, kterej jsem sem dával, tak pokud nechám zobrazovat jen ty náhodná čísla , tak se čísla zobrazujou.

int nahodne;
char *str[3] = {"prvni","druhe","treti"};
nahodne = rand() % 2+1;
this->label1->Text = System::Convert::ToString(nahodne);
#pragma once

#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#include "stdlib.h"
#include "string"

using namespace std;

namespace nahodnaslova {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;
	protected: 
	private: System::Windows::Forms::Label^  label1;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(378, 157);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 0;
			this->button1->Text = L"button1";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(379, 122);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(35, 13);
			this->label1->TabIndex = 1;
			this->label1->Text = L"label1";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(764, 326);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {


int nahodne;
string str[3] = {"prvni","druhe","treti"};
nahodne = (rand() % 3);
this->label1->Text = str[nahodne];

			 }
	};
}

Zpět do poradny Odpovědět na původní otázku Nahoru