Hi, how can i modify an array's element. For example, i have initiliazed the array[0] to 0 and when the user types something, then i want to become array[0]=1..and store the value.
any help?
Printable View
Hi, how can i modify an array's element. For example, i have initiliazed the array[0] to 0 and when the user types something, then i want to become array[0]=1..and store the value.
any help?
>> Hi, how can i modify an array's element.
You're pretty close....
array[0] = 1;
>> ...and when the user types something...
You'll have to be more specific if you need help with that part. If it's just getting input from the user in general, the FAQ contains some code you can study.
gg
You could also do this
Code:std::cin>>array[0];
Of course it's not quite good since you may enter "abcdefghij...." and it will try to store it in array[0] wich will crash the program but as long as you type only 1 character and press enter it will work.Code:#include <iostream>
int main() {
char array[1];
std::cout << "Enter whatever character you will and press enter: ";
std::cin >> array[0];
std::cout << "\nYou typed: " << array[0];
return 0;
}
Easy. Declare the array as an integer with ammount 0 as this:
.Code:int array[0]
Then set the value of the 1st place to equal 0 as this:
.Code:array[0]=0
Then ask for a user input using cin and a random variable, or cin.get(). After that set the 1st position of the array to equal 1 like this:
.Code:array[0]=1
Now array[0]==1.
i want to make a loop to modify me array's elements..when the user types 1 i want the array[0] be 1; and restart the program, when a second user types 1 i want the array[1] be 1...(all values starts from 0).
Try it out for yourself we showed you how and by re-start do you mean like close and exit and re-open or do you mean reset all variables to starting values
i want the modified values to be stored,when user types 1, and then go to starting menu without exit.. i can not figure a function to do that...
So you want to use a loop. You could then create temperary variables to hold the old values of the numbers
Code:int main(void)
{
int a = 0;
int tempA = 0;
int change = 0;
int exit = 0;
while(exit == 0)
{
tempA = a;
std::cout<<"A equals 0"<<std::endl;
std::cout<<"Enter a new value for a"<<std::endl;
std::cin>>a;
std::cout<<"Is this value ok [0] Yes [1] No"<<std::endl;
std::cin>>change;
if(change == 1)
{
a = tempA;
}
std::cout<<"Enter 0 to continue any other number to quit"<<std::endl;
std::cin>>exit;
}
return 0;
}
>Declare the array as an integer with ammount 0 as this
That's illegal. C++ requires that array sizes be greater than zero.
Ok I misworded it, I meant as 1 (which would be array[0]).Quote:
Originally Posted by Prelude
>I meant as 1 (which would be array[0]).
Your code was misworded too then:
Code:int array[0]; // Illegal
int array[1]; // Legal, but kind of silly because only array[0] is available
thanks bman for the code..really help me!
No, it will not. It will only store the 'a' in array[0].Quote:
Of course it's not quite good since you may enter "abcdefghij...." and it will try to store it in array[0]
could someone tell me how to save the a value when the 'while' statement starts again..?Code:#include <iostream>
#include <stdlib.h>
using namespace std;
void messageFunction();
void Titlos();
int function(int);
int main()
{
int a=0;
int num;
Titlos();
cout<<endl;
messageFunction();
cout<<"Choose an option:"<<endl;
cin>>num;
while (num==1&&a<5)
{
a++;
cout<<"You bought seat "<<a<<endl;
cin>>a;
break;
}
return main();
system("PAUSE");
return 0;
}
void messageFunction(void)
{
cout<<"Type 1 for First class"<<endl;
}
void Header()
{
cout<<"********Exercise 3 Solution********"<<endl;
}