Подробно объясним как исправить ту или иную ошибку самым простым способом на Oshibkino.ru
Error cin does not name a type
While trying to write this code, I get an error "cin doesnt name a type".
I don't know what the problem is exactly and I tried to write "using namespace std;"
but it gave the same error.
Here's the
While trying to write this code, I get an error "cin doesnt name a type".
I don’t know what the problem is exactly and I tried to write «using namespace std;»
but it gave the same error.
Here’s the code
#include<iostream>
namespace myStuff {
int value = 0;
}
using namespace myStuff;
int main {
std::cout << "enter integer " << ;
std::cin >> value;
std::cout << "nyouhaveenterd a value" << value ;
return 0;
}
Here’s the compilation error :
: extended initializer lists only available with `-std=c++0x` or `-std=gnu++0x` [enabled by default]|
: expected primary-expression before ‘;’ token|
expected `}` before `;` token|
`cin` does not name a type|
: `cout` does not name a type|
: expected unqualified-id before `return`|
: expected declaration before `}` token|
||=== Build finished: 6 errors, 1 warnings ===|
При попытке написать этот код я получаю сообщение об ошибке "cin doesnt name a type",
Я не знаю, в чем конкретно проблема, и я попытался написать «используя пространство имен std;», но он выдал ту же ошибку.
Вот код
#include<iostream>
namespace myStuff {
int value = 0;
}
using namespace myStuff;
int main {
std::cout << "enter integer " << ;
std::cin >> value;
std::cout << "nyouhaveenterd a value" << value ;
return 0;
}
Вот ошибка компиляции:
: extended initializer lists only available with `-std=c++0x` or `-std=gnu++0x` [enabled by default]|
: expected primary-expression before ‘;’ token|
expected `}` before `;` token|
`cin` does not name a type|
: `cout` does not name a type|
: expected unqualified-id before `return`|
: expected declaration before `}` token|
||=== Build finished: 6 errors, 1 warnings ===|
1
Решение
int main{
должно быть
int main(){
а также
std::cout << "enter integer " << ;
должно быть
std::cout << "enter integer ";
7
Другие решения
На этой линии:
std::cout << "enter integer " << ;
Там нет соответствующего операнда, чтобы сделать утверждение синтаксически допустимым. Это, вероятно, источник ваших ошибок.
1
Это предыдущая строка.
cout<<"enter integer" **<<** ;
что в прошлом << ожидает аргумент, который никогда не приводится
I tried the following program:
cin example
/**************************************
cin example
***************************************/
#include <iostream>
int main(void)
{
using namespace std;
int number;
cout<<«Please enter a number»<<endl;
cin >> number;
cout<<«your number is :»<<number<<endl;
return 0;
}
the g++ compiler gave me an error: ‘cin’ does not name a type
what am I doing wrong?
Re: problem with g++ cin
try removing the spaces…
Re: problem with g++ cin
Strange… I just compiled exactly what you pasted and everything worked for me.
Code:
roly@---:~/Desktop$ nano test.cpp
roly@---:~/Desktop$ g++ test.cpp
roly@---:~/Desktop$ ./a.out
Please enter a number
10
your number is :10
roly@---:~/Desktop$
Code:
roly@---:~/Desktop$ cat test.cpp
/**************************************
cin example
***************************************/
#include <iostream>
int main(void)
{
using namespace std;
int number;
cout<<"Please enter a number"<<endl;
cin >> number;
cout<<"your number is :"<<number<<endl;
return 0;
}
roly@---:~/Desktop$
Last edited by Rolcol; August 9th, 2008 at 12:19 AM.
Re: problem with g++ cin
I just found the problem.
I had typed the name of the program «cin example» as a first line in the file. When the compiler hit the cin it expected a type to be named.
thanks for the quick response
John
Краткий алгоритм реализации программы:
1. Объявить необходимые переменные, включая шаг по сетке h. Объявить
массивы соответствующего размера для x (будут храниться расчетные узлы) и
y_численное (значения искомой функции, которое вы найдёте в точках х по
формулам (3) и y_аналитическое (которое вы рассчитаете по данному
аналитическому решению в тех же расчетных точках). Длина этих трех
одномерных массивов, конечно же, совпадает. Объявить все дополнительные
переменные для хранения погрешностей и т.д.
2. Заполнить массив х расчетных узлов по формуле (5). Сохранить в файл
3. Рассчитать y_численное и y_аналитическое. Сохранить в файл.
4. Рассчитать значение абсолютной и относительной погрешности по формулам
(6-7). Вывести на экран.
#include <iostream>#include <cmath>#include <stdlib.h>#include <ctime>usingnamespace std;int i, N=10;double h, a, b,q,w,e;//pi=3.1415926535;double ant[N+1]={};//analytical value of ydouble num[N+1]={};//numerical value of ydouble x[N+1]={};// raschetnyy uzeldouble pg[N+1]={};cout<<"Our problem: dy/dx=y-e^x*sin(x), from -pi to pi."<<endl;// vyvod usloviyacout<<"Enter first(a) and last(b) value:"<<endl;// vvod otrezkacin>>a;cin>>b;
h=abs((b-a)/N);for(i=0; i<=N; i++){x[i]=a+i*h;}cout<<"Enter initial value for ant_y and num_y:";cin>>num[0];cin>>ant[0];cout<<"calculate the numerical value..."<<endl;for(i=1; i<=N; i++){num[i]=num[i-1]+h*(num[i-1]-(exp(x[i-1]))*sin(x[i-1]));}cout<<"calculate the analytical value..."<<endl;for(i=0; i<=N; i++){ant[i]=(exp(x[i]))*cos(x[i]);}cout<<" ready-made solution:"<<endl;cout<<"Xi Ch_Yi An_Yi"<,endl;for(i=0; i<=N; i++){cout<<"With i="<<i<<": "<<x[i]<<" "<<num[i]<<" "<<ant[i]<<endl;}for(i=0; i<=N; i++){pg[i]=abs(num[i]-ant[i]);}
q=pg[0];for(i=0; i<=N; i++){if(pg[i]>q){q=pg[i];}}cout<<"The absolute error is equal to "<<q<<endl;
w=ant[0];for(i=0; i<=N; i++){if(ant[i]>w){w=ant[i];}}
e=q/w;cout<<"The relative error is equal to "<<e<<endl;return0;}
Compilation failed due to following error(s). double ant[N+1]={}; //analytical value of y
^
main.cpp:11:15: error: array bound is not an integer constant before ‘]’ token
double num[N+1]={}; //numerical value of y
^
main.cpp:12:13: error: array bound is not an integer constant before ‘]’ token
double x[N+1]={};// raschetnyy uzel
^
main.cpp:13:28: error: array bound is not an integer constant before ‘]’ token
double pg[N+1]={};
^
main.cpp:14:1: error: ‘cout’ does not name a type
cout<<"Our problem: dy/dx=y-e^x*sin(x), from -pi to pi."<<endl;// vyvod usloviya
^~~~
main.cpp:15:1: error: ‘cout’ does not name a type
cout<<"Enter first(a) and last(b) value:"<<endl;// vvod otrezka
^~~~
main.cpp:16:1: error: ‘cin’ does not name a type
cin>>a;
^~~
main.cpp:17:1: error: ‘cin’ does not name a type
cin>>b;
^~~
main.cpp:18:1: error: ‘h’ does not name a typeh=abs((b-a)/N);
^
main.cpp:19:1: error: expected unqualified-id before ‘for’
for(i=0; i<=N; i++)
^~~
main.cpp:19:11: error: ‘i’ does not name a typefor(i=0; i<=N; i++)
^
main.cpp:19:17: error: ‘i’ does not name a typefor(i=0; i<=N; i++)
^
main.cpp:21:1: error: ‘cout’ does not name a type
cout<<"Enter initial value for ant_y and num_y:";
^~~~
main.cpp:22:1: error: ‘cin’ does not name a type
cin>>num[0];
^~~
main.cpp:23:1: error: ‘cin’ does not name a type
cin>>ant[0];
^~~
main.cpp:24:1: error: ‘cout’ does not name a type
cout<<"calculate the numerical value..."<<endl;
^~~~
main.cpp:25:1: error: expected unqualified-id before ‘for’
for(i=1; i<=N; i++)
^~~
main.cpp:25:11: error: ‘i’ does not name a typefor(i=1; i<=N; i++)
^
main.cpp:25:17: error: ‘i’ does not name a typefor(i=1; i<=N; i++)
^
main.cpp:27:1: error: ‘cout’ does not name a type
cout<<"calculate the analytical value..."<<endl;
^~~~
main.cpp:28:1: error: expected unqualified-id before ‘for’
for(i=0; i<=N; i++)
^~~
main.cpp:28:11: error: ‘i’ does not name a typefor(i=0; i<=N; i++)
^
main.cpp:28:17: error: ‘i’ does not name a typefor(i=0; i<=N; i++)
^
main.cpp:30:1: error: ‘cout’ does not name a type
cout<<" ready-made solution:"<<endl;
^~~~
main.cpp:31:1: error: ‘cout’ does not name a type
cout<<"Xi Ch_Yi An_Yi"<,endl;
^~~~
main.cpp:32:1: error: expected unqualified-id before ‘for’
for(i=0; i<=N; i++)
^~~
main.cpp:32:11: error: ‘i’ does not name a typefor(i=0; i<=N; i++)
^
main.cpp:32:17: error: ‘i’ does not name a typefor(i=0; i<=N; i++)
^
main.cpp:34:1: error: expected unqualified-id before ‘for’
for(i=0; i<=N; i++)
^~~
main.cpp:34:11: error: ‘i’ does not name a typefor(i=0; i<=N; i++)
^
main.cpp:34:17: error: ‘i’ does not name a typefor(i=0; i<=N; i++)
^
main.cpp:36:1: error: ‘q’ does not name a typeq=pg[0];
^
main.cpp:37:1: error: expected unqualified-id before ‘for’
for(i=0; i<=N; i++)
^~~
main.cpp:37:11: error: ‘i’ does not name a typefor(i=0; i<=N; i++)
^
main.cpp:37:17: error: ‘i’ does not name a typefor(i=0; i<=N; i++)
^
main.cpp:39:1: error: ‘cout’ does not name a type
cout<<"The absolute error is equal to "<<q<<endl;
^~~~
main.cpp:40:1: error: ‘w’ does not name a typew=ant[0];
^
main.cpp:41:1: error: expected unqualified-id before ‘for’
for(i=0; i<=N; i++){
^~~
main.cpp:41:11: error: ‘i’ does not name a typefor(i=0; i<=N; i++){
^
main.cpp:41:17: error: ‘i’ does not name a typefor(i=0; i<=N; i++){
^
main.cpp:44:1: error: ‘e’ does not name a typee=q/w;
^
main.cpp:45:1: error: ‘cout’ does not name a type
cout<<"The relative error is equal to "<<e<<endl;
^~~~
main.cpp:46:1: error: expected unqualified-id before ‘return’
return0;
^~~~~~
main.cpp:47:1: error: expected declaration before ‘}’ token
}
^
__________________
Помощь в написании контрольных, курсовых и дипломных работ, диссертаций здесь
0
Yetty
7423 / 5018 / 2890
Регистрация: 18.12.2017
Сообщений: 15,694
21.06.2021, 18:01
2
Сообщение было отмечено Sara9 как решение
Решение
после строки 5 добавьте строки
C++
1
2
int main(){
и исправьте опечатку в строке 30 — вместо <, напишите <<