Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-14-2005, 03:43 PM   #1 (permalink)
superbeastie
Registered User
 
Join Date: Apr 2005
Posts: 6
superbeastie is on a distinguished road
Question 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.
superbeastie is offline   Reply With Quote
Old 04-14-2005, 05:03 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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;
}
__________________
Valmont is offline   Reply With Quote
Old 04-14-2005, 11:28 PM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-15-2005, 11:39 AM   #4 (permalink)
superbeastie
Registered User
 
Join Date: Apr 2005
Posts: 6
superbeastie is on a distinguished road
Thankyou for your help both of you, I dont know why I didn;t think of that before, just my noobishness I suppose .
superbeastie is offline   Reply With Quote
Old 04-15-2005, 11:45 AM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Actually, for an unseasoned programmer, this might be tricky indeed. I am not surprised.
__________________
Valmont is offline   Reply With Quote
Old 04-15-2005, 09:45 PM   #6 (permalink)
destin
Code Monkey
 
Join Date: Mar 2005
Location: NJ
Posts: 40
destin is on a distinguished road
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;
}
destin is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 06:24 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting