Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm just making a program and this happens
I don't understand where is converts like that.
This program is supposed to see how many of each character is in a input string. It is work in progress but I already made a for loop to process the input.
Error:
11:30: error: conversion from 'int' to non-scalar type 'std::vector<int>' requested
Code:
// Example program
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string input;
vector<int> letters = (26,0);
vector<char> alpha = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
cin >> input;
for(int i = 0; i < input.size();i++){
for(int j = 0; j < alpha.size(); j++){
if(input[i]==alpha[j]){
letters[j] ++;
}
}
}
}
Your mistake is that you try to call constructor with 2 parameters, using assign without explicitly showing what are that 2 ints mean. You can choose 2 ways of this implementation:
vector<int> letters = vector<int>(26, 0);//here you call copy constructor
vector<int> letters(26,0); // here you call constructor with 2 parameters
Compiler just don't know what do you mean writing "vector letters = (26,0);" it's could be everything - from just 1 int number to implicit convertion to your classes; So you should to show it explicitly.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
A very short program
#include <iostream>
using namespace std;
int main()
{
cin>> int x;
return 0;
}
it's not compiling, why can't declare after cin?
You first have to declare a variable before using it. Or in other words: Variables have to be in scope when using them. That's the way it works in C++.
cin is the object in c++ of class istream.
It is Used to accept the input from input devices like keyboard and
int x; is declaration of variable i.e. allocating the space where our number from input stream will get stored.>> is extraction operator which receives the stream.So declaration and taking input are 2 different things. 1) allocate memory(int x)2)write into it.
These 2 things won't happen together.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
this program is simple :
1)take an input string .
2) convert it to long .
3) print convert result.
expected an output,but nothing found.
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
string ch;
scanf("%s",ch);
long l=stol(ch);
printf("%l",l);
return 0;
}
Here's how it's done with C++ I/O. There's very little reason for using C I/O in a C++ program.
#include <iostream>
#include <string>
int main()
{
std::string input;
std::cin >> input; // take an input string
long lval = stol(str); // convert to long
std::cout << lval << '\n' // print the result
}
Now this stuff would be covered in the first chapter of any C++ book. A good book will greatly increase how quickly and how well you learn C++.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am making a simple program, please take a look
#include<iostream>
using namespace std;
int main(int argc,char* argv[])
{
int op=0;
int v[20]=[1, 0];
float Ma=0;
if (argv[1]==1)
{
float S=0;
for(int i=0;i<=20;i++)
{
S=S+v[i];
}
Ma=(double)(S/20);
}
cout<<"Media aritmetica pentru elementele din vector este "<<Ma<<endl;
return 0;
}
I get this error Program.cpp:10:13: error: expected identifier before numeric constant
int v[20]=[1, 0];
^
I am using the gcc from ubuntu for compiling and I'm not really sure if there's anything to it that may cause this. I am a bit new into this.
You probably meant to define an array of 20 int's and initialize its first 2 elements to 1 and 0 respectively.
Well, instead of writing:
int v[20] = [1, 0];
you should have written:
int v[20] = {1, 0};
which means what you want it to mean. Note, however, that the term "vector" has a different meaning typically in C++ - the name of the std::vector container class in the standard library.
Use curly braces instead.
int v[20]={1,0};
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I've 10 numbers to be read. My task is to generate exceptions with the numbers are either a negative number or a even number. Below is the code I wrote, but it isn't working.
#include <iostream>
using namespace std;
int main()
{
int a[10];
for(int i=0;i<10;i++)
{
cin>>a[i];
}
for(int i=0;i<10;i++)
{
try{
if(a[i]<0 && a%2==0)
throw a[i];
}
catch(int a)
{
cout<<"You ve entered a -ve number or a even number";
}
}
return 0;
}
This was the error shown:
In function 'int main()': 16:24: error: invalid operands of types 'int [10]' and 'int' to binary 'operator%'
Thanks for the help!
Two things:
You are modulo-ing an array a with an integer 2. You should dereference it to get the value: a[i] % 2 == 0.
You do a boolean AND, where you want a boolean OR (according to your question): || instead of &&.
Typo here. You can not do modulo operation on an array. a%2==0 should be a[i]%2==0. Also change && to || as you are trying to find "either a negative number or a even number".
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I cannot find out what's wrong in this code:
using namespace std;
const int sz = 10000+100;
int sqr[sz];
int digit;
void findSqr()
{
for(int i = 0; i <= 10000; i++);
{
cout<<sqr[i]<<endl;
}
}
int main()
{
findSqr();
return 0;
}
When I'm initializing int I in for loop in findsqr function and trying to build the code then an error appears
name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]|
How should I fix it?
Remove the semicolon from the end of the for statement, that is, replace
for(int i = 0; i <= 10000; i++);
with
for(int i = 0; i <= 10000; i++)
Nevertheless, bear in mind that loops without a body (i.e. not followed by curly braces) are also possible C language constructs. These are followed by a semicolon.