set values to charachter arrays which are entered [modified]
-
hey, is there any way to set values to charachter arrays that you enter for example:
int main()
{
int a1, a2, a3, a4,...........a11, x;cin >> a1;............cin >> a11;
char str[10][40]
for(x = 0; x < 10; x++){
gets(str[x]);
}int "str[0]" = a1, "str[1]" = a2,.........................."str[9]" = a11;
return 0;
}thanks
modified on Sunday, September 7, 2008 8:06 PM
-
hey, is there any way to set values to charachter arrays that you enter for example:
int main()
{
int a1, a2, a3, a4,...........a11, x;cin >> a1;............cin >> a11;
char str[10][40]
for(x = 0; x < 10; x++){
gets(str[x]);
}int "str[0]" = a1, "str[1]" = a2,.........................."str[9]" = a11;
return 0;
}thanks
modified on Sunday, September 7, 2008 8:06 PM
use the pointer:
int a[10];
for(x=0;x<10;x++)
{
= a[x];
}You know some birds are not meant to be caged, their feathers are just too bright.
-
use the pointer:
int a[10];
for(x=0;x<10;x++)
{
= a[x];
}You know some birds are not meant to be caged, their feathers are just too bright.
#include "stdafx.h"
#include "iostream"
#include "cstring"
using namespace std;int main()
{
int x, y;
char ppl[5][80];for(y = 0; y < 5; y++){ gets\_s(ppl\[y\]); } for(x = 0; x < y; x++){ cout << ppl\[x\] << "\\n"; } int a\[5\]; for(x = 0;x < y; x++){ \*ppl\[x\] = a\[x\]; } for(x = 0;x < y; x++){ cout << a\[x\] << "\\n"; } return 0;
}
compile this and see what the values for each of the strings are, it doesn't work. and how do you output the values when the variable name is the same as the name for the string, for example if you want to display the strings showing what their values are.
-
#include "stdafx.h"
#include "iostream"
#include "cstring"
using namespace std;int main()
{
int x, y;
char ppl[5][80];for(y = 0; y < 5; y++){ gets\_s(ppl\[y\]); } for(x = 0; x < y; x++){ cout << ppl\[x\] << "\\n"; } int a\[5\]; for(x = 0;x < y; x++){ \*ppl\[x\] = a\[x\]; } for(x = 0;x < y; x++){ cout << a\[x\] << "\\n"; } return 0;
}
compile this and see what the values for each of the strings are, it doesn't work. and how do you output the values when the variable name is the same as the name for the string, for example if you want to display the strings showing what their values are.
toprogramminguy wrote:
for example if you want to display the strings showing what their values are.
toprogramminguy wrote:
<< "\n";You have made it here.
toprogramminguy wrote:
int a[5];
initialize it just like cin>>a[i]..... Well,if you do really want to convert from "int (like a[x])" to "char [](like ppl[x])",what you have related before.(like "str[0]" = a1,"str[1] = a2............").(The compiler does not allow you to do like this ,but you do.) You can try this:
sprintf(str[i],"%d",a[i]);//#include "stdio.h"
//sprintf(str[i],"%c",a[i]);You know some birds are not meant to be caged, their feathers are just too bright.
modified on Monday, September 8, 2008 10:25 PM
-
toprogramminguy wrote:
for example if you want to display the strings showing what their values are.
toprogramminguy wrote:
<< "\n";You have made it here.
toprogramminguy wrote:
int a[5];
initialize it just like cin>>a[i]..... Well,if you do really want to convert from "int (like a[x])" to "char [](like ppl[x])",what you have related before.(like "str[0]" = a1,"str[1] = a2............").(The compiler does not allow you to do like this ,but you do.) You can try this:
sprintf(str[i],"%d",a[i]);//#include "stdio.h"
//sprintf(str[i],"%c",a[i]);You know some birds are not meant to be caged, their feathers are just too bright.
modified on Monday, September 8, 2008 10:25 PM
Thanks ill try that but can you set each of the strings be a variable name and then set a value, as opposed to setting a value to a variable name then setting the string equal to that variable name, if you can understand me and my terrible way of explaining things. I'm only learning c++ by myself so i haven't got a clue really thanks again
-
Thanks ill try that but can you set each of the strings be a variable name and then set a value, as opposed to setting a value to a variable name then setting the string equal to that variable name, if you can understand me and my terrible way of explaining things. I'm only learning c++ by myself so i haven't got a clue really thanks again
#include <string>//not <string.h>
using namespace std;
string str1,str2;
str1 = "this is my first string.";
str2 = "this is my second string."
cout<<str1<<endl;
cout<<str2<<endl;String is a class in boost c++ library,and it has many member functions to deal with string,you can do a search about it,or find a book contains chapter string.
You know some birds are not meant to be caged, their feathers are just too bright.