Why is this code not affecting the output? - c++

This is supposed to take the input and move each letter 1 to the right. Is the pause stopping it from doing anything?
How can I change it so that it doesn't just output what the user inputs?
This is the code in C++ in Visual Studio Community 2013:
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string>
#include <cctype>
int _tmain(int argc, _TCHAR* argv[])
{
string cyphertext;
cout << "Paste your cyphertext and press enter to shift right 1: ";
cin >> cyphertext;
void encrypt(char * cyphertext, unsigned int offset);
for (int i = 0; cyphertext[i] != 0; i++) {
char firstLetter = islower(cyphertext[i]) ? 'a' : 'A';
unsigned int alphaOffset = cyphertext[i] - firstLetter;
int offset = 0;
unsigned int newAlphaOffset = alphaOffset + offset;
cyphertext[i] = firstLetter + newAlphaOffset % 26;
cout << "" << "Right One: " << cyphertext;
system("pause");
return 0;
}
}

Your pause is inside the 'encrypting' loop. It needs to be outside. The return in the loop will terminate the program; that too needs to be outside the loop.
Note that it is easier to see such mistakes when the code is indented in an orthodox layout such as the one now in the question. Working with scratty layouts makes it really hard to see many problems that are obvious when the code is tidily laid out.
You also declare a function encrypt() which you never use; don't do that. And it's generally a bad idea to declare functions inside other functions. And given that there isn't an encrypt() function defined, there isn't a 'void function', so I've changed the question title for you.

Related

I want to reverse a text using these functions but i dont know what went wrong

#include <iostream>
#include <string.h>
using namespace std;
void reverse_Text(char* str);
int main()
{
int i, j, Text, revText;
const int [arrSize] = 101;
char Text[arrSize], revText[arrSize];
cout << "Please enter a sentence: ";
cin >> Text;
reverse_Text(Text);
cout << "The reversed text is " << Text << endl;
return 0;
}
int reverse_Text(char ori_str, char rev_str)
{
int i = 0;
for(j = strlen(ori_str)-i, j >= 0, j--){
rev_str[i] = ori_str[j];
rev_str[i+1] = '0';
i += 1;
}
}
I cant seem to find the error, im still a newbie in coding, it says that text and revtext are not declared, i cant find the next error when there is already an error at the top
int reverse_Text(char ori_str, char rev_str)
This function takes two characters as parameters. You actually need to pass the addresses of the characters.
int reverse_Text(const char* ori_str, char* rev_str)
Notice that I've declared the first parameter as const. This tells the compiler that the original string is not supposed to be changed. The compiler will then generate an error if any code inside the function tries to modify any character in the original string.
You will need to pass both parameters, not just one, when you call the function.
reverse_Text(Text, revText);
This is actually passing the address of the first character in each string.

Strange output from C++ in Linux Terminal

I've recently started learning programming using the C++ language. I wrote a simple program that is supposed to reverse a string which I compile in the Terminal using gcc/g++.
#include <iostream>
using namespace std;
string reverse_string(string str)
{
string newstring = "";
int index = -1;
while (str.length() != newstring.length())
{
newstring.append(1, str[index]);
index -= 1;
}
return newstring;
}
int main()
{
string x;
cout << "Type something: "; cin >> x;
string s = reverse_string(x);
cout << s << endl;
return 0;
}
I've rewritten it multiple times but I always get the same output:
Type something: banana
��
Has anyone had a problem like this or know how to fix it?
Your code initializes index to -1, and then uses str[index] but a negative index has no rational meaning in C++. Try instead initializing it like so:
index = str.length() - 1;
I can see several issues with your code. Firstly, you are initializing index to -1, and then decrementing it. Maybe you meant auto index = str.length()-1;?
I recommend you look at std::reverse, which will do the job you're after.
Your main function then becomes:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string x;
cout << "Type something: ";
cin >> x;
reverse(x.begin(), x.end());
cout << x << endl;
return 0;
}
If you really want to write your own reverse function, I recommend iterators over array indices. See std::reverse_iterator for another approach.
Note, the above will simply reverse the order of bytes within the string. Whilst this is fine for ASCII, it will not work for multi-byte encodings, such as UTF-8.
You should use a memory debugger like valgrind.
It's a good practice to scan your binary with it, and will make you save so much time.

Can someone explain the error in this while loop?

So I'm a beginner programmer... and I can't figure out what the problem is in this bit of code I'm writing for a text adventure. All I want it do At the moment is let the user enter a command, and then it converts it to ALLCAPS and prints that out. It should output this:
What shall I do?
pie
Your raw command was: PIE
But instead, it outputs this:
What shall I do?
pie
PIE
...and then it freezes. Here's the code:
#include <iostream>
#include <string>
#include <cctype>
#include <cstring>
#include <vector>
using namespace std;
void command_case();
string userIn;
string raw_command;
int x = 0;
int main()
{
while(raw_command != "QUIT")
{
cout << "What shall I do?\n";
cin >> userIn;
command_case();
cout << "Your raw command was: " << raw_command << endl;
}
return 0;
}
void command_case()
{
char command[userIn.size()+1];
strcpy(command, userIn.c_str());
while(x < userIn.size()+1)
{
if(islower(command[x]))
{
command[x] = toupper(command[x]);
cout << command[x];
x++;
}
else if(isupper(command[x]))
{
cout << command[x];
x++;
}
}
raw_command = command;
}
I think it may be a problem with the while loop in void command_case(), but I can't figure out exactly what that problem is. I'd appreciate any advice you can give me.
One too much:
while(x < userIn.size()+1)
The problem is with the x variable in the command_case() function.
When x becomes 3 (and "command[x] points to the null character at the end of "pie")
neither islower(command[x]) or isupper(command[x]) are true.
Neither section of the if statement executes, so x stays at 3 forever.
Since "userIn.size()+1" is 4, and x never reaches 4, the loop never exits.
A possible solution is remove the "x++" from both sections of the if statement, and have a single "x++" after the if statement. This will increment x during every loop regardless of what character "command[x]" points to.
You could easily do something like
void command_case()
{
for(int i =0; i<userIn.size(); i++)
{
userIn[i] = toupper(userIn[i]);
}
}
then cout<<userIn in the main
You should remove all cout calls from command_case() function. In fact the whole if-branch in the function is useless and you could just replace it with the following:
command[x]=toupper(command[x]);
For the simplicity you could replace the whole command_case() function with (just remember to #include <algorithm>):
std::transform(userIn.begin(), userIn.end(), userIn.begin(), toupper);

C++: reading a string, converting to dynamic int array

I am writing a program that asks the user to type in a very large int (much larger than the type int can handle). When receive this int from the user, it is stored in a string. Then, I want to convert this string into an int array (I am using a dynamic int array). After compiling and running the program, I get values that don't make sense. The values of my int array seem to be random gibberish. I don't see why this is so - it doesn't look like my loops are out of bound in the converting process. Please help. The purpose of creating an int array is to then come up with ways to add, subtract, multiply, and compare very large int values. To make it clear what I am intending to do: say the user types in "12345". I want to store this string value into an int array that would have a length of 5, each element corresponding to the next number in the int.
largeIntegers.h
#ifndef H_largeIntegers
#define H_largeIntegers
#include <iostream>
#include <string>
class largeIntegers
{
private:
void readInteger();
// reads integer
public:
std::string s_integer;
int* integer;
int length;
largeIntegers();
// default constructor
void outputInteger();
// outputs integer
};
#endif
largeIntegers.cpp
#include <iostream>
#include <string>
#include "largeIntegers.h"
using namespace std;
largeIntegers::largeIntegers()
{
readInteger();
}
void largeIntegers::readInteger()
{
int i = 0,j = 0, k;
cout << "Enter large integer: ";
cin >> s_integer;
for (; s_integer[i] != '\0'; i++);
length = i;
int* integer = new int[i];
k = 0;
for (j = i - 1; j >= 0; j--)
integer[j] = s_integer[k++] - 48;
}
void largeIntegers::outputInteger()
{
for (int i = length - 1; i >= 0; i--)
cout << integer[i];
}
User.cpp
#include <iostream>
#include <string>
#include "largeIntegers.h"
using namespace std;
int main()
{
largeIntegers a;
cout << a.length << endl << endl;
cout << a.integer[0] << endl << a.integer[1] << endl;
a.outputInteger();
cout << endl << endl;
return 0;
}
I intentionally made the variables in the header public for debugging purposes. My output on the console after compiling is:
Enter large integer: 111
3
952402760
1096565083
10966961571096565083952402760
This is the problem
int* integer = new int[i];
change to
integer = new int[i];
Your version declares a local variable that just happens to have the same name as your class variable. Easy mistake to make.
also, using standards facilities like std::vector and std::getline would make your code much cleaner in addition to avoid the problem you had, and resolve memory leaks you have now if you call readInterger twice:
void largeIntegers::readInteger()
{
cout << "Enter large integer: ";
std::getline(std::cin, s_integer);
integer = std::vector(s_integer.size());
//your last loop to fill the array probably can be replaced by std::transform
}

c++ count down script

#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int num, num2;
num = 33;
do
{
cout <<"\n" <<num-3;
}
while (num >=3);
system("PAUSE");
return EXIT_SUCCESS;
}
I have coded the above but when I run it, it outputs 30 and does not deplete the value to 3.
How can I have the loop do this? I know that num-- would work but that would only deplete the value by one. I'm new to c++ and I am trying to figure these things out.
Thanks! :)
//edit thanks I have it working now with num = num - 3, num-=3 works too
this line:
cout <<"\n" <<num-3;
does not change the value of num. It just outputs the value of num - 3. To actually change the value you need another line, such as:
num -= 3;
Even if you do as suggested and subtract 3 (or whatever) every iteration of your loop, it may not do a whole lot of good. The problem is fairly simple: you're likely updating your variable a lot faster than the output can be printed, so you may easily see a dozen or more values all appear at essentially the same time.
To cure that, you generally want to pause for a short time between iterations, so one value will (probably) be visible before the next is printed. Based on the system("pause");, I'm going to guess you're running Windows, in which case code something like this may be a bit more to your liking:
#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc, char *argv[])
{
int num = 33;
do
{
cout <<" \r" << (num-=3);
Sleep(100);
}
while (num >=3);
return EXIT_SUCCESS;
}