Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Am I Doing This Right?

c++ string realloc ascii value

  • Please log in to reply
16 replies to this topic

#13 Hydrokr0n1k

Hydrokr0n1k

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 57 posts
  • Learning:C++, JavaScript

Posted 28 April 2012 - 06:33 AM

ok put some spacing in the code I have it all nice and neat in my compiler lol but it removes the indents when I copy and paste how do I make it stop that? as well switched gets with gets_s and that fixed one debugging problem, it is still telling me that fopen is unsafe any other options for that? and then I thought I understood what to do on the int main then I think after what you just typed I really have to rethink it so then what I would have to do is......
#include <stdio.h>
void reversenoloop(int, char); //theres no cchar or int in it though
													 //unless you count int reverse no loop
int main()
   {
reversenoloop(forward, userInput); //so we can use it here
   }
//code for everything down here?


of course this being modeled off of the reversenoloop function that has no intergers or charachters?

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
using namespace std;
void complexReverseString();
void reversenoloop();
int main()
{
reversenoloop();
complexReverseString();
}

int complexReverseString(string userInput)
{
string source(userInput);
string target( source.rbegin(), source.rend() );
cout << "The reversed string is " << target << endl;
return 0;
}
int reversenoloop()
{
ifstream input;
string forward;
cout << "Please enter a string to see its reverse. (Will display reverse twice)" << endl;
input.open("information.txt");
cin >> forward;
//reverseString(forward, findStringSize(forward)); a function I'm not actually calling
input >> forward;
complexReverseString(forward);

input.close();
system ("pause");
}

its not working I am definitly doing something wrong here
  • 0

#14 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 28 April 2012 - 12:45 PM

Currently, your forward declarations do not match your actual definitions, and you are feeding the compiler false hints as to the functions you will be using.

Try to match them up and see why. If you modify what the definition (the actual function's code block) is, such as to return something else, or to accept a second or third parameter you *must* update the forward declaration.

The whole point is so that that compiler understands what the function will accept, be named, and return so that it can be called anywhere (such as in main(), where you wish the program to use those functions) and feeding it the wrong parameters or return type will cause it to write wrong code, and so it must halt.

After fixing that you have one more error: try to understand and fix it too. It may require adding some more code, however it should run after that.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#15 robbiewoods05

robbiewoods05

    CC Regular

  • Member
  • PipPipPip
  • 40 posts
  • Learning:C++, C#, Haskell

Posted 28 April 2012 - 04:05 PM

It might be easier if you declare the methods before main() so that you don't need to use forward declarations (unless you have to).


So do something like:

#include "stdafx.h"
#include <iostream>

using namespace std;

void writeAnswer(int x)
{
	  cout << x << endl;
}

int main()
{
	  int x = 45, y = 453;
	  writeAnswer(x + y);
	  system("PAUSE");
	  return 0;
}

But if you have to use forward declarations its done like:

#include "stdafx.h"
#include <iostream>

using namespace std;

void writeAnswer(int x);

int main()
{
	  int x = 45, y = 453;
	  writeAnswer(x + y);
	  system("PAUSE");
	  return 0;
}

void writeAnswer(int x)
{
	  cout << x << endl;
}

Hope I could help.
  • 0

#16 Hydrokr0n1k

Hydrokr0n1k

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 57 posts
  • Learning:C++, JavaScript

Posted 29 April 2012 - 03:08 PM

so I am confused what if Ido not know what char I am declaring I looked through my code and I have so many lol

int c = strlen(a) - 1;
cin >> forward;
int reverse()
char nextChar;
char String[100];
int Sum,Index;

just to name a few so do I need to

void function they are in(char nextChar, char String[100])
void another function that others are in(int Sum,Index, int c = strlen(a))

int main()
{
	 char nextChar;
		  function they are in(char nextChar);
	 int Sum,Index;
		  another function that others are in(int Sum,Index);


	 char String[100];
		  function they are in(char String[100]);
	 cin >> forward;
		  function they are in(cin >> forward);
			   return 0;
}
and I have to use forward declarations when doing multiple functions right?
  • 0

#17 robbiewoods05

robbiewoods05

    CC Regular

  • Member
  • PipPipPip
  • 40 posts
  • Learning:C++, C#, Haskell

Posted 30 April 2012 - 07:15 AM

You're confusing yourself. With all due respect, I think you need to read a few more tutorials. Try http://www.learncpp.com, it's what I used. Go through each section and make sure you understand it before you go on to the next.
  • 0





Also tagged with one or more of these keywords: c++, string, realloc, ascii value

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download