|
Consider the two chunks of code separately.
The first two lines are updating angle by a fraction of turnSpeed. If speed were == maxSpeed then speed/maxSpeed = 1. So angle += turnSpeed for a right-turn. turnSpeed = 0.08 so that's in Radians which is 4.58 degrees. This means if you're turning right or left at max speed, it'll add or remove 4.58 degrees each time through. If speed is less than the max, then the angle will change by a smaller amount (a fraction of .08 radians). Try it in the debugger and see the change.
The last two lines are updating the x,y location of the car using it's orientation (angle). cos(angle) is typically the x portion of an angle so I'm not sure why it modifies the y coor with that but that's what it does. sin(angle) or cos(angle) produces a value from 0-1 (which it uses as a percentage) * speed. That means x or y changes by a fraction of speed in the direction of angle.
It would probably help you to print out the values or use the debugger.
Hope this helps,
John
|
|
|
|
|
|
In native code, we often use helper classes to save a reference to a member, set its value and reset it in destruction. standard stuff.
Unfortunately, I haven't found a good way in a ref class to do the same and I'm hoping for some tips here.
Since I cannot hold a reference to a member of a managed object as a member of another object, I tried using a tracking reference. Unfortunately, that results in an similar error:
error C3160: 'System::Boolean ^%': a data member of a managed class cannot have this type
note: an interior reference can never be allocated on the gc heap
An example I hope to get working...
ref class Foo { bool m_guard; . . . }
ref class PushBool sealed {
bool ^% m_tref;
public:
PushBool(bool ^% b, bool newVal) : m_tref(b), m_oldval(b) { b = newVal; }
~PushBool() { m_tref = m_oldval; }
};
usage...
void Foo::work() {
if (m_guard) return;
PushBool no_reentry(m_guard, true);
...
}
Do any of the C++/CLI devs here have advice on handling this? I can probably concoct a way to make it work but was hoping for a reusable utility class.
John
|
|
|
|
|
Hello guys
I've have a c++ source code for a litle phoonbook.
It has the insert option, View and delete but i've can't edit a record!
Here is source code please add a edit part for this code as possible.
Also it does not going back to main menu after adding a record.
thanks
<pre>#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define NAMES 50
#define EMAIL 20
#define PHONE 20
#define ADDRESS 8
#define MOBILE 10
#define SIZE 500
void welcome_note();
void record();
void view();
void search();
void del_rec();
char info[SIZE];
struct addressbk
{
char name [NAMES] ;
char sname [NAMES] ;
char emailadd [EMAIL] ;
char address [ADDRESS];
char mobile [MOBILE];
char phone [PHONE];
char codemelli [PHONE];
} addressbk;
FILE *Records;
FILE *rectemp;
void main()
{
int choice=0;
system("cls");
system("color 17");
fflush(stdin);
welcome_note();
printf("\nEnter your Choice here \t");
scanf("%i",&choice);
fflush(stdin);
switch (choice)
{
case 1:
{
system("cls");
view();
break;
}
case 2:
{
system("cls");
record();
break;
}
case 3:
{ system("cls");
search();
break;
}
case 4:
{ system("cls");
del_rec();
break;
}
case 5:
{
system("cls");
break;
}
default:
{
printf("Please Only Choose Between NO. 1-5\n"); system("pause");
system("cls");
main();
}
}
}
void record()
{
char choice2;
do
{ Records = fopen("Records.txt","a+");
printf("Enter Name\n");
fflush(stdin);
scanf("%s",addressbk.name);
printf("Enter Second Name\n");
fflush(stdin);
scanf("%s",addressbk.sname);
printf("Enter E-mail address\n");
fflush(stdin);
scanf("%s",addressbk.emailadd);
printf("Enter address\n");
fflush(stdin);
scanf("%s",addressbk.address);
printf("Enter mobile\n");
fflush(stdin);
scanf("%s",addressbk.mobile);
printf("Enter Code Melli no\n");
fflush(stdin);
scanf("%s",addressbk.codemelli);
printf("Enter Phone Number no\n");
fflush(stdin);
scanf("%s",addressbk.phone);
fprintf(Records,"%s %s %s %s %s %s %s \n",addressbk.name,addressbk.sname,addressbk.emailadd,addressbk.address,addressbk.mobile,addressbk.codemelli,addressbk.phone);
fflush(stdin);
fclose(Records);
printf("WOULD LIKE TO CONTINUE? Y/N \n");
scanf("%c",&choice2);
fflush(stdin);
if (choice2=='n'||choice2=='N')
{
system("cls");
main();
}
}while(choice2=='y'||choice2=='Y');
}
void view()
{
char choice3;
Records = fopen("Records.txt","a+");
do
{
fgets(info,SIZE,Records);
printf("%s\n",info);
}while(!feof(Records));
fclose(Records);
system("pause");
printf("Search for records??(Y/N)\n");
scanf("%c",&choice3);
fflush(stdin);
if (toupper(choice3)=='Y')
{
search();
}
else if(toupper(choice3)=='N')
{
fclose(Records);
system("pause");
system("cls");
main();
}
else
{
fclose(Records);
system("cls");
}
}
void welcome_note()
{
printf("\t\t Phone BOOK V:1 \n\n\n");
printf("\t\t ProGrammeR: Fatemeh \n\n\n");
printf("\t\t ----------------------------------------\n");
printf("\t\t - CHOOSE ONE BETWEEN (1-5) -\n");
printf("\t\t ----------------------------------------\n");
printf("\t\t - 1 : VIEW RECORDS -\n\n");
printf("\t\t - 2 : MAKE NEW ENTRIES -\n\n");
printf("\t\t - 3 : SEARCH RECORDS -\n\n");
printf("\t\t - 4 : DELETE RECORDS -\n\n");
printf("\t\t - 5 : EXIT -\n\n");
printf("\t\t ----------------------------------------\n");
}
void search()
{
char Target[SIZE];
int Found=0;
if((Records=fopen("Records.txt","r"))==NULL)
printf("Recordi Peyda Nashod!!!\n\n");
else
{
system("cls");
printf("\n\n");
printf("\t----------------- SEARCH --------------");
printf("\n\n");
printf("\tEnter Code Melli to search:");
fflush(stdin);
scanf("%s",Target);
while(!feof(Records)&& Found==0)
{
fscanf(Records,"%s %s %s %s %s %s %s",addressbk.name,addressbk.sname,addressbk.emailadd,addressbk.address,addressbk.mobile,addressbk.codemelli,addressbk.phone);
if(strcmp(Target,addressbk.codemelli)==0)
Found=1;
}
if(Found)
{
printf(".......Record Peyda Shod\n");
system("pause");
system("cls");
printf("1 FILE FOUND : \n\n\n");
printf("First Name: %s\n",addressbk.name);
printf("Last Name: %s\n",addressbk.sname);
printf("E-mail: %s\n",addressbk.emailadd);
printf("Address : %s\n",addressbk.address);
printf("mobile: %s\n",addressbk.mobile);
printf("Code Melli: %s\n",addressbk.codemelli);
printf("Phone Number: %s\n",addressbk.phone);
system("pause");
main();
}
else if(!Found)
{
printf("Motasfim Recordi Peyda Nashod\n");
system("pause");
main();
}
fclose(Records);
}
}
void del_rec()
{
char Target[SIZE];
int Found=0;
system("cls");
rectemp=fopen("rectemp.txt","w");
if((Records=fopen("Records.txt","r"))==NULL)
printf("RECORDI YAFT NASHOD!!!\n\n");
else{
printf("\n\n");
printf("\t--------------- DELETE ----------------");
printf("\n\n");
printf("\tEnter Code Melli to Delete: ");
fflush(stdin);
gets(Target);
while(!feof(Records))
{
fscanf(Records,"%s %s %s %s %s %s %s ",addressbk.name,addressbk.sname,addressbk.emailadd,addressbk.address,addressbk.mobile,addressbk.codemelli,addressbk.phone);
if(feof(Records))
break;
if(strcmp(Target,addressbk.codemelli)!=0)
fprintf(rectemp,"%s %s %s %s %s %s %s \n",addressbk.name,addressbk.sname,addressbk.emailadd,addressbk.address,addressbk.mobile,addressbk.codemelli,addressbk.phone);
else {
Found=1;
printf(".......Record Peyda Shod \n");
system("pause");
printf("First Name:%s\n",addressbk.name);
printf("Last Name:%s\n",addressbk.sname);
printf("E-mail:%s\n",addressbk.emailadd);
printf("Address : %s\n",addressbk.address);
printf("mobile: %s\n",addressbk.mobile);
printf("Code Melli: %s\n",addressbk.codemelli);
printf("Phone Number: %s\n",addressbk.phone);
fflush(stdin);
system("pause");
}
}
if (!Found)
{
printf("\tRECORD NOT FOUND\n"); }
printf("\tRECORD DELETED!!\n");
fclose(Records);
fclose(rectemp);
remove("Records.txt");
rename("rectemp.txt","Records.txt");
main(); }
}
|
|
|
|
|
Sorry, this site does not provide code to order. Please edit your question and explain what does not work, and where in the code the problem lies. It would also help if you removed all the code that is not related to the problem.
|
|
|
|
|
I'm not going to write the complete edit function for you. But you already have most of the parts because editing can be implemented by selecting, reading, entering new data, deleting the old record, and inserting the edited data.
Quote: Also it does not going back to main menu after adding a record. Never call main() from within your code as done here in the record() function:
if (choice2=='n'||choice2=='N')
{
system("cls");
main();
} Just return from the function (simply delete the above code block). Then execution continues after the function call in main() . Similar for the other functions where you call main() too.
To process the next operation you have to implement a loop in main() that gets and processes the next choice until the exit option is selected:
do
{
printf("\nEnter your Choice here \t");
scanf("%i",&choice);
fflush(stdin);
switch (choice)
{
}
system("cls");
} while (choice != 5);
Note also that this the wrong forum because this is about Managed C++/CLI as stated in the topmost (sticky) post. Please use the C / C++ / MFC Discussion Boards[^] or Ask a Question for further C/C++ related questions where the latter is the better choice when posting larger amounts of code (which should be avoided).
|
|
|
|
|
Hi so this is my first post here, I'm having the following problem
----------------------------------------------------------------------------------------------------
6 11 E:\Trabalhos\Programação\Projeto\Projeto.cpp [Error] expected constructor, destructor, or type conversion before '(' token
----------------------------------------------------------------------------------------------------
Here's the code:
#include <iostream>
#include <fstream>
#include <locale.h>
using namespace std;
setlocale (LC_ALL, "Portuguese");
ofstream ficheiro ("Info Trabalhador");
int x, i,a,b,d;
int esc[4] = {100, 150, 200, 250};
struct trab
{
char nome[100];
char morada[200];
int numero;
int telefone;
};
trab f;
int salario(int n1,int n2){
int money;
money = n1*n2;
return money;
}
void menu1 (){
cout<<" * * * * * * * * * * * * * * * * * * * *"<<endl;
cout<<" * 1. - Identificação *"<<endl;
cout<<" * 2. - Escalão Do Trabalhor *"<<endl;
cout<<" * 3. - Número De Horas Trabalhadas *"<<endl;
cout<<" * 4. - Salvar Ficheiro *"<<endl;
cout<<" * 5. - Sair *"<<endl;
cout<<" * * * * * * * * * * * * * * * * * * * *"<<endl;
}
int main (){
menu:
menu1();
cout<<endl;
cout<<"R: ";
cin>>x;
cout<<endl;
switch (x)
{
case 1:
cout<<"Qual o seu nome?"<<endl<<endl;
cout<<"R: ";
cin>>f.nome;
cout<<endl;
cout<<"Qual a sua morada?"<<endl<<endl;
cout<<"R: ";
cin>>f.morada;
cout<<endl;
cout<<"Qual o seu número?"<<endl<<endl;
cout<<"R: ";
cin>>f.numero;
cout<<endl;
cout<<"Qual o seu telemóvel?"<<endl<<endl;
cout<<"R: ";
cin>>f.telefone;
cout<<endl;
system("pause");
system("cls");
goto menu;
break;
case 2:
for (i=0; i<=4; i++)
{
cout<<i+1 <<". "<<esc[i]<<" euros/horas"<<endl;
cout<<endl;
}
cout<<"R: ";
cin>>a;
cout<<endl;
system ("Pause");
system ("cls");
goto menu;
case 3:
cout<<"Quantas horas trabalhou?"<<endl;
cout<<"R: ";
cin>>b;
if (a == 1)
{
d=100;
cout<<"Deve Receber: "<<salario(d,b)<<endl<<endl;
system ("Pause");
system ("cls");
goto menu;
}
else if (a == 2)
{
d=150;
cout<<"Deve Receber: "<<salario(d,b)<<endl<<endl;
system ("Pause");
system ("cls");
goto menu;
}
else if (a==3)
{
d=200;
cout<<"Deve Receber: "<<salario(d,b)<<endl<<endl;
system ("Pause");
system ("cls");
goto menu;
}
else
d=250;
cout<<"Deve Receber: "<<salario(d,b)<<endl<<endl;
system ("Pause");
system ("cls");
goto menu;
break;
case 4:
if(ficheiro.is_open())
{
ficheiro<<"Nome: "<<f.nome<<endl;
ficheiro<<"Morada: "<<f.morada<<endl;
ficheiro<<"Número: "<<f.numero<<endl;
ficheiro<<"Telemóvel: "<<f.telefone<<endl;
break;
}
case 5:
return 0;
break;
}
system ("Pause");
return 0;
}
Thanks for your help.
|
|
|
|
|
You are declaring local executable statements outside functions which is incorrect. You should move then inside an appropriate function, probably main .
modified 13-May-18 7:36am.
|
|
|
|
|
I have a small program. I compile the executable. If I find the executable
and double click on it, everything works fine. I can print, print preview; all is
well.
If within visual studio I hit the green triangle to run the executable and then I
try to print or print preview, I get an error in this code:
inline void* CThreadSlotData::GetThreadValue(int nSlot)
{
EnterCriticalSection(&m_sect);
ASSERT(nSlot != 0 && nSlot < m_nMax);
ASSERT(m_pSlotData != NULL);
ASSERT(m_pSlotData[nSlot].dwFlags & SLOT_USED);
ASSERT(m_tlsIndex != (DWORD)-1);
if( nSlot <= 0 || nSlot >= m_nMax )
{
LeaveCriticalSection(&m_sect);
return NULL;
}
CThreadData* pData = (CThreadData*)TlsGetValue(m_tlsIndex);
if (pData == NULL || nSlot >= pData->nCount)
{
LeaveCriticalSection(&m_sect);
return NULL;
}
void* pRetVal = pData->pData[nSlot];
LeaveCriticalSection(&m_sect);
return pRetVal;
}
It breaks at LeaveCriticalSection(). Im really perplexed as to why this error has arisen
and what to do to get it to go away. If you have any insight, please let me know.
|
|
|
|
|
It would help if you gave us the details of the error you're getting. There's no way we can guess.
Also, what line of code does it occur on? You have LeaveCriticalSection in three different lines.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
i am trying to make a game which requires a constant input from user, i looked through net but didn't understand it. How can i get an constant input from user while the program is running like
_kbhit_ does.
|
|
|
|
|
|
Hi,
I have a small project in vc++ 2015. While I calculate the total, I get this error.
Is it possible to solve this error. Any Kind helps.
private: static System::Decimal MyNumber2;
private: static System::Decimal DJSubTot = 0;
private: static System::Decimal DJVATTot = 0;
private: static System::Decimal DJFreightTot = 0;
MyNumber2 = 0;
if(Decimal::TryParse(textBox8->Text->Trim(), MyNumber2)){
DJVATTot = MyNumber2;
}
MyNumber2 = 0;
if (Decimal::TryParse(textBox9->Text->Trim(), MyNumber2)) {
if (MyNumber2>0) {
DJFreightTot = MyNumber2;
}
}
textBox10->Text = (DJSubTot + DJVATTot + DJFreightTot).ToString();
Thanks for the helps
|
|
|
|
|
I don't have C++/CLI, but converting that code to C# it works fine. Where does the error occur?
|
|
|
|
|
Thank Richard
textBox10->Text = (DJSubTot + DJVATTot + DJFreightTot).ToString();
The above line
Thank Again
|
|
|
|
|
I have had another look at the documentation and cannot see why the error occurs. As I already mentioned, I do not have C++/CLI (and don't want it) on my system, so cannot test it further.
|
|
|
|
|
It's ok Richard, Thank Again for reply
The following code clears my error()
textBox10->Text = Convert::ToString(Decimal::Add(Decimal::Add(DJSubTot, DJVATTot), DJFreightTot));
modified 13-Apr-18 21:22pm.
|
|
|
|
|
Hi,
I have a small project in VS2010 and its working fine. Now I am trying to upgrade to VS 2015 & Found Visual C++ by using creating project C++/Cli->UI->Windows Forms Applications.
I get struck in following simple codes, I cannot identify my mistake.
public ref class My_ProjectData
{
public:
static System::Data::DataTable^ UnitRateTbl = gcnew System::Data::DataTable();
public: static void MyUnitRate_Collect() {
String^ ICode=String::Empty;
for(int W1=0; W1<=UnitRateTbl->Rows->Count-1; W1++){
ICode=UnitRateTbl->Rows[W1]["itm_code"]->ToString(); }
};
Error : function "System::Data::DataRowCollection::default[int]::get" cannot be called with the given argument list
Thanks for the helps
|
|
|
|
|
I have looked at the documentation and cannot see an error in that line of code. However, I notice that you have declared UnitRateTbl as static , and wonder if that may be the problem.
|
|
|
|
|
Thank Richard,
I checked by Without static declaration, but still the error raised.
I assume that, we have to supply the datatype to that for loop variable [W1].[May be Not sure]
String^ ICode=String::Empty;
for(int W1=0; W1<=UnitRateTbl->Rows->Count-1; W1++){
ICode=UnitRateTbl->Rows[<int^>W1]["itm_code"]->ToString(); ??????????????
}
Thanks Again
|
|
|
|
|
That will not help since the data type is already defined in your for expression. You could try breaking the statement into its parts:
DataRow row = UnitRateTbl->Rows[W1];
ICode= row["itm_code"];
... and see what happens.
|
|
|
|
|
|
Okay ...I was working on making my own string class using dynamic memory allocation. The things were working fine. But when I sent the same code to one of my friends the code crashed. He was using the same compiler. The then copied pasted the same code in another source file, so it crashed. Then I changed the name of the original file, again program crashed. But I changed to name again to previous one and then again program was working fine. so whats this whole thing. c++.
P.S: I have dynamic memory and pointers.
|
|
|
|
|
Member 13767759 wrote: so whats this whole thing. You think we can somehow guess?
|
|
|
|
|
You should use "Remote Desktop" to dial into your friend's computer, do the "paste" thing, and then tell them what bozos they were for not following your instructions (assuming they work).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|