|
 |
|
 |
04-14-2005, 03:43 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 6
|
String/Array Help
I suppose the easiet way for me to explain is using some strange C++/pseudo code: :p
#include<stdio.h>
#include<string.h>
int num;
char txt = "string";
char string1[10] = "Test1";
char string2[10] = "Test2";
char string3[10] = "Test3";
....etc....
char string18[10] = "Test";
printf("choose a number from 1 to 18");
scanf("%d", num);
printf("%s",txt+num);
so basicly if the user inputs 9, string 9 will appear, or if the user inputs 2, string 2 will appear.
So How would I go about getting something like that to work in C++.
I hope I explained that well enough, forgive me for my noobishness.
|
|
|
04-14-2005, 05:03 PM
|
#2 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Your code looks more like C instead of C++, but here is the C++ version.
Code:
#include <iostream>
#include <string>
//-
using std::string;
using std::endl;
using std::cout;
using std::cin;
//-
int main()
{
string s1("test 1");
string s2("test 2");
string s3("test 3");
//etc...
string MyStringArray[3]={s1, s2, s3};
cout<<"Choose a number from 1 to 3"<<endl;
unsigned idx;
cin>>idx;
cin.get();
if(idx > 0 && idx <= 3)
{
cout<<MyStringArray[idx-1]<<endl;
}
return 0;
}
__________________
|
|
|
04-14-2005, 11:28 PM
|
#3 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
Here is a C version:
Code:
#include <stdio.h>
int main()
{
char** strings = {"test 1", "test2", "test3", NULL};
int select = 0;
printf("Choose a number from 1 to 3\n");
if(1 != fscanf(stdin, "%d\n", &select))
{
printf("Some error happened durign read\n");
return -1;
}
if(select > 0 && select <= 3)
{
printf("Your selection: %s\n", strings[select]);
}
return 0;
}
Altho it hasn't been tested, it shuld work right out of the box.
|
|
|
04-15-2005, 11:39 AM
|
#4 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 6
|
Thankyou for your help both of you, I dont know why I didn;t think of that before, just my noobishness I suppose  .
|
|
|
04-15-2005, 11:45 AM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Actually, for an unseasoned programmer, this might be tricky indeed. I am not surprised.
__________________
|
|
|
04-15-2005, 09:45 PM
|
#6 (permalink)
|
|
Code Monkey
Join Date: Mar 2005
Location: NJ
Posts: 40
|
Here's my version in C...
Code:
#include <stdio.h>
int main()
{
char *strings[] = {NULL, "Oggala Booggala!", "Lalalalala", "Huh!?"};
int select;
do {
printf("Choose a number from 1 to 3: ");
scanf("%i", &select);
}
while (select > 3 || select < 1);
if(select > 0 && select <= 3)
{
printf("You selected \"%s\".\n", strings[select]);
}
return 0;
}
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 08:48 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|