Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to declare
int x = it must be more or equal to 1 but less or equal to 100;
How can I do it? I dont want to use if condition, Im looking for something short and clear, if possible.
The x number is input, so program should accept only numbers in this limit.
It seems that you're looking to error check on initialization.
If I were you I'd do something along the lines of.
int x;
cout << "Enter a value: " << flush;
cin >> x;
while(!((x>=1)&&(x<=100))) {
cout << "Try Again: " << flush;
cin >> x;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 months ago.
Improve this question
How to create a code in c++ that if I insert any number "n" it displays all the "even" numbers that are smaller or equal to "n"
I dont have much knowledge in C++ so I would appreciate the help
This is what I tried and think it's good enough
int n;
cout << "\nEnter number= ";
cin >> n;
cout << "\nEven numbers are ";
for(int i = 0; i <= n; i++)
{
if ( i % 2 == 0 )
{
cout << i <<" ";
}
}
You can use a repetition loop to check for all the numbers until N and the look if each number is divisible by 2 (when, in that division, the remainder is 0).
#john ideia of starting counting at 2 and going up 2 at a time is a easier one.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
#include <iostream>
#include <string>
int main(){
int a, b;
std::cin >> a;
std::cin.ignore(3, '/');
std::cin >> b;
std::cout << a << " " << b;
return 0;
}
How can I change my code so that the separators do not have to be Slash characters “/” but any separator that is not an integer number.
You can't use ignore for that.
Instead use a loop to peek at and fetch the next character until the character matches your condition to stop "ignoring" character.
The characters you read that you want to "ignore", just don't do anything with them.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to write a program that takes a string and an integer in a class object. The program will then sort the class objects, in an array of objects, by the integer, allowing me to then display the names. Unfortunately, when I try to build the array, I have an error on my assignment operator.
My questions are: Do I need to overload the = operator, and if so, how (somehow I've never figured out how to overload operators)? If not, where am I going wrong?
Here is the code:
void InitiativeList::makeList(size_type physicalSize, size_type logicalSize)
{
string sNewActor;
int iNewOrder;
for (size_t index = 0; index < physicalSize; index++)
{
if (logicalSize == physicalSize)
{
grow(physicalSize);
}
cout << "Enter character name: ";
cin >> sNewActor;
if (sNewActor == "Exit")
{
return;
}
cout << "Enter initiative roll: ";
cin >> iNewOrder;
actorOrder[index] = new Actor(iNewOrder, sNewActor);
logicalSize++;
}
}
Thank you for your help.
You don't need new there, because you have an array of actorData and not actorData pointers, and the error is saying that it can't convert actorData pointer to actorData. So replace this line:
actorOrder[index] = new actorData(iNewOrder, sNewActor);
with this:
actorOrder[index] = actorData(iNewOrder, sNewActor);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I know how to use a string to calculate the number of characters, but I'm not sure how to use a function to do that. have to use CSTRING. THANK YOU ALL
#include <cstring>
char a[10];
cout << "Please enter anything: ";
cin.getline(a,10);
cout << "You type " << strlen(a) << " letters long"<<endl;
You're probably looking for std::string since you're question mentioned C++ and not only C.
include <string>
std::string myString = "Something";
size_t stringLength = myString.size();
It's simple. Just type your code inside a function()
int stringlengthfunction()
{
char str[80];
int i;
cout<<"\n enter string:";
cin.getline(str,80);
int n=strlen(str);
cout<<"\n lenght is:"<<n;
getch();
return 0;
}
or pass your string as a parameter to the function
int stringlengthfunction(string str)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Im newbie in c++ programming. How can I do something like this?..
int question1;
question1: "What is your name?";
to set the text value in integer?
#include <string>
#include <iostream>
int main( )
{
std::string name;
std::cout << "What is your name?: " << std::endl;
std::cin >> name;
std::cout << "Your Name: " << name << std::endl;
std::cin.get( );
return 0;
}
Simply read the input in as string.