我是C++的新手并尝试制作两个简单的函数,但出了点问题.
我正在尝试执行以下操作:
1.Function for input some data.
2.Function to show what data is input.
Run Code Online (Sandbox Code Playgroud)
我只是想简单一点.我到目前为止写的代码是:
#include <iostream>
void masiv()
{
int x[10];
int n, i;
int min;
int max=0, imax=0, imin;
cout << "Enter the number of elements: ";
cin >> n;
for(i=0; i < n; i++)
{
cout << "Input value for x["<<i<<"]=";
cin >> x[i];
if (min > x[i])
{
min = x [i];
imin = i;
}
if (max < x[i])
{
max = x[i];
imax = …Run Code Online (Sandbox Code Playgroud) 我有两个文件,print_permu.c和gen_permu.cpp.我希望将print_permu.c文件编译成目标文件,然后使用目标文件进行编译gen_permu.cpp,其中包含对函数的调用print_permu.c.
print_permu.c
#include<stdlib.h>
#include<stdio.h>
typedef char byte;
char *printable=NULL;
size_t pos,size,*char_cnt;
void __print_permu_recurse()
{
if( pos==size )
{
printf("%s\n",printable);
return;
}
byte iter = 25;
while( iter>=0 )
{
if( char_cnt[iter] )
{
printable[pos] = 'a'+iter;
--char_cnt[iter];
++pos;
__print_permu_recurse();
--pos;
++char_cnt[iter];
}
--iter;
}
}
void print_permu(size_t *char_count)
{
char_cnt = char_count;
for(pos = 0,size = 0 ; pos<26 ; ++pos)
size += char_count[pos];
printable = (char*)malloc(sizeof(char)*(size+1));
printable[size] = '\0'; …Run Code Online (Sandbox Code Playgroud) 我有一个php文件,其中包含另一个使用include()
我$something在包含文件中定义了一个变量的文件,它将根据在包含文件中运行的函数而改变.
现在,我想在原始文件中打印该变量,当我使用echo $something它时打印绝对没什么,帮忙吗?
我想使用PHP声明一个全局变量,并在函数内部使用.
我试过了:
$var = "something";
function foo()
{
echo $var;
}
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误,指出$var未定义.
我怎么解决这个问题?
对不起,这是一个基本问题,但我的所有研究都几乎没有回答我的问题,我只是想在我编写所有代码之前仔细检查.我正在使用C,我有一个头文件,其中声明了一些变量(ints,char数组,int数组).如果我有一个使用这些全局变量的函数,我假设我可以使用变量而不传递它们?
示例头文件:
int state;
int lnArray[];
Run Code Online (Sandbox Code Playgroud)
C文件:
void function(){
int i;
for(i=0;i<5;i++;){
if(lnArray[i]<10)
state = lnArray[i];
}
}
Run Code Online (Sandbox Code Playgroud) 我想在MFC应用程序中声明一个全局变量,以便应用程序内的每个动作都可以看到该变量并对其进行操作,但是我不知道在哪里可以确切地声明该变量。在这段代码中,我有许多按钮动作,我需要声明一个在这些动作之间共享的字符串变量。
// CalculatorDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Calculator.h"
#include "CalculatorDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
public:
static CString myValue;
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) {}
void CAboutDlg::DoDataExchange(CDataExchange* pDX){
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CCalculatorDlg dialog
CCalculatorDlg::CCalculatorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCalculatorDlg::IDD, …Run Code Online (Sandbox Code Playgroud) 我是一个新手程序员,现在正在使用Visual Studio 2010和许多表单开发C#应用程序,所以我感兴趣的是可以设置一些全局变量以在所有形式中使用它们吗?简单的例子,我有10个表单,所有这些都连接到MySQL数据库,因此在理想中设置10次数据库名称和密码并不是真的很聪明我可以设置一次然后随处使用,我找不到答案谷歌的这个问题,所以也许有人可以帮助我吗?可能吗?
我是c的初学者.我被赋予了一个任务,检查用户是否可以用3个角度的输入形成三角形,并且还必须将三角形的类型(例如斜角,斜角等)打印到屏幕上.我需要分两部分执行此操作,并尝试在主要部分中执行第一部分,同时将第二部分声明为函数.
但是,似乎我不能在函数中使用主函数中使用的相同变量而不再声明它.有没有办法实现这个目标?
我用Google搜索并了解了全局变量,然而,它是否真的适用于这种情况,因为变量的值是用户输入,因此无法在主脚本和其他函数之外执行?
这是我的代码:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
double ang1,ang2,ang3;
printf("This program determines if a triangle can be formed with an input of 3 angles.\n");
printf("Enter the first angle: ");
scanf("%lf",&ang1);
printf("\nEnter the second angle: ");
scanf("%lf",&ang2);
printf("\nEnter the third angle: ");
scanf("%lf",&ang3);
if (ang1<=0.000000 || ang2<=0.000000 || ang3<=0.000000){//handles any angles below or equal to 0 degrees
printf("\nError 404: Angle(s) are not above 0 degrees.\nPlease restart the program again\n");
}
else{
if (ang1+ang2+ang3==180.000000){ //checks if angles add up to …Run Code Online (Sandbox Code Playgroud) 在C++中,建议在主程序内部或其他地方之前声明全局变量?我的意思是,有什么区别
#include <iostream>
int variable;
int main()
{ //my program
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和
#include <iostream>
int main()
{
int variable;
//my program
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在哪种情况下我应该使用哪一个?
I cannot find why it isn't working. It outputs the number I inputted but with 1 digit less. Ex: 12345 ---> 1234.
I have already tried changing the while loop for a for one adding ifs and removing the parentheses.
#include <iostream>
using namespace std;
int num0, num1, x, y, z, num2;
int main()
{
cout << "input your number \n";
cin >> num0;
y = 0;
x = 1;
z = -1;
num2 = 0;
while (num0 > y) …Run Code Online (Sandbox Code Playgroud)