Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,172 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,927 people online right now. Registration is fast and FREE... Join Now!




s undeclared

 
Reply to this topicStart new topic

s undeclared, I can't figure this error message out. Any suggestions?

Susan0814
6 Dec, 2007 - 07:17 PM
Post #1

New D.I.C Head
*

Joined: 25 Nov, 2007
Posts: 2


My Contributions
This is part of a modular program that converts numbers to roman numerals and vice a versa and I need to get it to compile before putting it into the other part of the program. The other half of the program works fine but this half keeps sending an error that s is undeclared. The program is an exact duplicate (only broken into halves) of a problem from a programming contest and it will compile and run when compiled as one. We are working in pairs and neither of us can figure this one out. What are we missing? Any suggestions? All declarations are exactly as written in the original program. The original problem can be found at: http://acm.ashland.edu/2007/Problem-Set/Problems/real.pdf; it is problem A.

CODE

#include <iostream>
#include <string>
using namespace std;
int main(void){
int toInt(string s);
{
    int ans=0;
    int prev = 0;
    for (int i = s.length()-1; i >= 0; i--)
    {
        int temp = 0;
        char ch = s[i];
        switch (ch)
        {
        case 'I' : temp = i; break;
        case 'V' : temp = 5; break;
        case 'X' : temp = 10; break;
        case 'L' : temp = 50; break;
        case 'C' : temp = 100; break;
        case 'D' : temp = 500; break;
        case 'M' : temp = 1000; break;
        }
        if (temp >= prev)
           ans += temp;
        else
           ans -= temp;
        prev = temp;
        }
        return ans;
}                    


This post has been edited by Susan0814: 6 Dec, 2007 - 07:38 PM
User is offlineProfile CardPM
+Quote Post

dontKnowJava
RE: S Undeclared
6 Dec, 2007 - 07:24 PM
Post #2

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 213


My Contributions
im not a c++ expert but if it tell you s is undeclared then declare it
User is offlineProfile CardPM
+Quote Post

nirvanarupali
RE: S Undeclared
7 Dec, 2007 - 01:48 AM
Post #3

D.I.C Foot
Group Icon

Joined: 1 Aug, 2007
Posts: 983



Thanked: 2 times
Dream Kudos: 375
My Contributions
You cannot implement a function within the main function or any function. All you have to is declare it, then implement it then call it in function main.

CODE
#include <iostream>
#include <string>
using namespace std;

int toInt(string s); //function declaration

//main function, your program starts here
int main()

{
    toInt(name); //call a function
    
    return 0;  //exit success
}

//function declaration
int toInt(string s);
{
    ...codes here
}            
    
    
    

User is offlineProfile CardPM
+Quote Post

Bench
RE: S Undeclared
7 Dec, 2007 - 02:28 AM
Post #4

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 617



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(Susan0814 @ 7 Dec, 2007 - 03:17 AM) *

CODE

int toInt(string s);
{    



Assuming you're intending that code block to become part of your toInt function, then you need to remove the semicolon from the end of the function's signature.

You also need to separate your toInt function away from int main(), as nirvanarupali pointed out.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: S Undeclared
7 Dec, 2007 - 03:55 AM
Post #5

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,449



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
Why don't you simply use the atoi() function found in stdlib.h? Designing your own wheel is nice, but sometimes one that is already designed is rounder, smoother, & more precise than your own.
User is online!Profile CardPM
+Quote Post

Bench
RE: S Undeclared
7 Dec, 2007 - 10:14 AM
Post #6

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 617



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
atoi is unreliable when an error is encountered. The preferred way in C++ is the stringstream library. in 'C', you should use sscanf.
CODE
  string my_string = "1234";
  int num;
  stringstream ss;
  ss << my_string;

  if( ss >> num )
    //successful conversion

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:37AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month