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 ===|

David G's user avatar

David G

93.3k41 gold badges165 silver badges250 bronze badges

asked Dec 3, 2012 at 23:46

Ahmed Sherif's user avatar

int main{

should be

int main(){

and

std::cout << "enter integer " << ;

should be

std::cout << "enter integer ";

answered Dec 3, 2012 at 23:49

Luchian Grigore's user avatar

Luchian GrigoreLuchian Grigore

251k63 gold badges454 silver badges619 bronze badges

1

On this line:

std::cout << "enter integer " << ;

There’s no corresponding operand to make the statement syntactically valid. That’s probably the source of your errors.

answered Dec 3, 2012 at 23:49

David G's user avatar

David GDavid G

93.3k41 gold badges165 silver badges250 bronze badges

1

Its the previous line.

 cout<<"enter integer" **<<** ;

that last << is expecting an argument which is never given

answered Dec 3, 2012 at 23:50

DanChianucci's user avatar

DanChianucciDanChianucci

1,1652 gold badges11 silver badges21 bronze badges

1

При попытке написать этот код я получаю сообщение об ошибке "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" **<<** ;

что в прошлом << ожидает аргумент, который никогда не приводится

0

  • Home
  • Forum
  • The Ubuntu Forum Community
  • Ubuntu Official Flavours Support
  • New to Ubuntu
  • [ubuntu] problem with g++ cin

  1. problem with g++ cin

    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?


  2. Re: problem with g++ cin

    try removing the spaces…


  3. 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.


  4. 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


Bookmarks

Bookmarks


Posting Permissions

Sara9

0 / 0 / 0

Регистрация: 20.06.2021

Сообщений: 6

1

В чём ошибка при написании кода?

21.06.2021, 17:53. Показов 1201. Ответов 1

Метки c++, массивы c++ (Все метки)


Краткий алгоритм реализации программы:
1. Объявить необходимые переменные, включая шаг по сетке h. Объявить
массивы соответствующего размера для x (будут храниться расчетные узлы) и
y_численное (значения искомой функции, которое вы найдёте в точках х по
формулам (3) и y_аналитическое (которое вы рассчитаете по данному
аналитическому решению в тех же расчетных точках). Длина этих трех
одномерных массивов, конечно же, совпадает. Объявить все дополнительные
переменные для хранения погрешностей и т.д.
2. Заполнить массив х расчетных узлов по формуле (5). Сохранить в файл
3. Рассчитать y_численное и y_аналитическое. Сохранить в файл.
4. Рассчитать значение абсолютной и относительной погрешности по формулам
(6-7). Вывести на экран.

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <ctime>
using namespace std;
int i, N=10;
double h, a, b,q,w,e;
//pi=3.1415926535;
double ant[N+1]={}; //analytical value of y
double num[N+1]={}; //numerical value of y
double x[N+1]={};// raschetnyy uzel
              double pg[N+1]={};
cout<<"Our problem: dy/dx=y-e^x*sin(x), from -pi to pi."<<endl;// vyvod usloviya
cout<<"Enter first(a) and last(b) value:"<<endl;// vvod otrezka
cin>>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;
return 0;
}

Bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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 type
 h=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 type
 for (i=0; i<=N; i++)
           ^
main.cpp:19:17: error: ‘i’ does not name a type
 for (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 type
 for (i=1; i<=N; i++)
           ^
main.cpp:25:17: error: ‘i’ does not name a type
 for (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 type
 for (i=0; i<=N; i++)
           ^
main.cpp:28:17: error: ‘i’ does not name a type
 for (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 type
 for (i=0; i<=N; i++)
           ^
main.cpp:32:17: error: ‘i’ does not name a type
 for (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 type
 for (i=0; i<=N; i++)
           ^
main.cpp:34:17: error: ‘i’ does not name a type
 for (i=0; i<=N; i++)
                 ^
main.cpp:36:1: error: ‘q’ does not name a type
 q=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 type
 for (i=0; i<=N; i++)
           ^
main.cpp:37:17: error: ‘i’ does not name a type
 for (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 type
 w=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 type
 for (i=0; i<=N; i++){
           ^
main.cpp:41:17: error: ‘i’ does not name a type
 for (i=0; i<=N; i++){
                 ^
main.cpp:44:1: error: ‘e’ does not name a type
 e=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’
 return 0;
 ^~~~~~
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 — вместо <, напишите <<



1



Понравилась статья? Поделить с друзьями:

Читайте также:

  • Error checking type of the expression
  • Error checking tls connection host is not running
  • Error checking tls connection docker
  • Error checking storage configuration
  • Error checking mode

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии